Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > SOS! how to search & replace some string to numbers in a text file?

Reply
Thread Tools

SOS! how to search & replace some string to numbers in a text file?

 
 
walala
Guest
Posts: n/a
 
      09-21-2003
Dear all
I want to search and replace some string in input file A.txt and then output
B.txt.


The target string in A.txt is:¡°NRD=1U/X.XXE-0X¡±,(where X.XX stands for a
number, -E0X is its exponentials, for example, NRD=1U/1.84E-06)

I want to change this to ¡°NRD=Y.YYYY¡±, where Y.YYYY=1e-6/X.XXe-0X.

For example, "NRD=1U/1.84E-06" -> "NRD=0.5435"

I want to search the whole file and for all such occurence, replace then
output to B.txt.

Can anybody give me the example code on how to do this? Thanks a lot!

-Walala


 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      09-21-2003
walala wrote:

....


> I want to search and replace some string in input file A.txt and then output
> B.txt.
>
>
> The target string in A.txt is:¡°NRD=1U/X.XXE-0X¡±,(where X.XX stands for a
> number, -E0X is its exponentials, for example, NRD=1U/1.84E-06)
>
> I want to change this to ¡°NRD=Y.YYYY¡±, where Y.YYYY=1e-6/X.XXe-0X.
>
> For example, "NRD=1U/1.84E-06" -> "NRD=0.5435
>
> I want to search the whole file and for all such occurence, replace then
> output to B.txt.
>
> Can anybody give me the example code on how to do this? Thanks a lot!
>
> -Walala



One one line [untested]:


perl -pe
's|NRD=1U/(\d\.\d\dE[+-]?0\d)|"NRD=".sprintf("%6.4f",1e-6/$1)|eg' A.txt
>B.txt


Change quotes around if on Windoze.
--
Bob Walton

 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      09-21-2003
walala wrote:

....


> I want to search and replace some string in input file A.txt and then output
> B.txt.
>
>
> The target string in A.txt is:¡°NRD=1U/X.XXE-0X¡±,(where X.XX stands for a
> number, -E0X is its exponentials, for example, NRD=1U/1.84E-06)
>
> I want to change this to ¡°NRD=Y.YYYY¡±, where Y.YYYY=1e-6/X.XXe-0X.
>
> For example, "NRD=1U/1.84E-06" -> "NRD=0.5435"
>
> I want to search the whole file and for all such occurence, replace then
> output to B.txt.
>
> Can anybody give me the example code on how to do this? Thanks a lot!
>
> -Walala
>



All on one line [untested]:


perl -pe
's|NRD=1U/(\d\.\d\dE[+-]?0\d)|"NRD=".sprintf("%6.4f",1e-6/$1)|eg' A.txt
>B.txt


--
Bob Walton

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-21-2003
walala wrote:
>
> I want to search and replace some string in input file A.txt and then output
> B.txt.
>
> The target string in A.txt is:¡°NRD=1U/X.XXE-0X¡±,(where X.XX stands for a
> number, -E0X is its exponentials, for example, NRD=1U/1.84E-06)
>
> I want to change this to ¡°NRD=Y.YYYY¡±, where Y.YYYY=1e-6/X.XXe-0X.
>
> For example, "NRD=1U/1.84E-06" -> "NRD=0.5435"
>
> I want to search the whole file and for all such occurence, replace then
> output to B.txt.
>
> Can anybody give me the example code on how to do this? Thanks a lot!



perl -pe's!(?<=NRD=)1U/(\d\.\d+[Ee]-\d+)!sprintf"%.4f",1e-6/$1!eg' A.txt > B.txt



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: OT: Text editors (was Re: Search and replace text in XML file?) Tim Chase Python 10 08-31-2012 03:56 AM
OT: Text editors (was Re: Search and replace text in XML file?) Chris Angelico Python 9 07-29-2012 05:55 PM
Finding 1000 largest numbers from a file having some billion numbers Subra C Programming 25 03-08-2007 01:31 AM
some stupid questions about string search & replace in perl walala Perl Misc 21 09-23-2003 06:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57