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

W

walala

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
 
B

Bob Walton

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

Change quotes around if on Windoze.
 
B

Bob Walton

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
 
J

John W. Krahn

walala said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top