Professional Web Applications Themes

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

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...

  1. #1

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

    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 Guest

  2. #2

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

    walala wrote:

    ....

     


    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.
    --
    Bob Walton

    Bob Guest

  3. #3

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

    walala wrote:

    ....

     


    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 

    --
    Bob Walton

    Bob Guest

  4. #4

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

    walala wrote: 


    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 Guest

Similar Threads

  1. Search and replace pattern in a file
    By Perl in forum PERL Beginners
    Replies: 2
    Last Post: January 20th, 01:04 AM
  2. search and replace in complexed text file?
    By Nick Ellson in forum PERL Beginners
    Replies: 2
    Last Post: November 22nd, 03:15 AM
  3. Search string in a file
    By Chad Fowler in forum Ruby
    Replies: 7
    Last Post: October 21st, 07:23 PM
  4. replace string in file
    By Niels Bosboom in forum PHP Development
    Replies: 7
    Last Post: September 29th, 06:41 PM
  5. How to replace a long string in a text file?
    By Tor Hildrum in forum PERL Beginners
    Replies: 2
    Last Post: August 12th, 05:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139