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

Discussion in 'Perl Misc' started by walala, Sep 21, 2003.

  1. walala

    walala Guest

    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
    walala, Sep 21, 2003
    #1
    1. Advertising

  2. walala

    Bob Walton Guest

    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
    Bob Walton, Sep 21, 2003
    #2
    1. Advertising

  3. walala

    Bob Walton Guest

    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
    Bob Walton, Sep 21, 2003
    #3
  4. 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
    John W. Krahn, Sep 21, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subra
    Replies:
    25
    Views:
    1,174
    user923005
    Mar 8, 2007
  2. walala
    Replies:
    21
    Views:
    206
    John W. Krahn
    Sep 23, 2003
  3. walala
    Replies:
    3
    Views:
    235
    Mina Naguib
    Sep 22, 2003
  4. Chris Angelico
    Replies:
    9
    Views:
    227
    Andrew Cooper
    Jul 29, 2012
  5. Tim Chase
    Replies:
    10
    Views:
    366
    Robert Miles
    Aug 31, 2012
Loading...

Share This Page