string substitution help (search and replace)

Discussion in 'Perl Misc' started by Ray Muforosky, Mar 9, 2005.

  1. Hello everyone I need help.

    I need to do string substitution on over 350 files on the following
    string. Is there a tricky command in vi or command line that can do
    this.

    FILE CONTENT:
    ......
    .....
    kasj tgn=0507, thr=12, options=sched,
    safa tgn=0504, thr=22, options=sched,
    asda tgn=0513, thr=122, options=sched,
    asca tgn=055, thr=562, options=sched,
    asca tgn=0514, thr=42, options=sched,

    where there is "thr=###" I need make it "thr=##" deleting the first
    digit.
    So line 3 and 4 will be changed and the file content will be:

    kasj tgn=0507, thr=12, options=sched,
    safa tgn=0504, thr=22, options=sched,
    asda tgn=0513, thr=22, options=sched,
    asca tgn=055, thr=62, options=sched,
    asca tgn=0514, thr=42, options=sched,

    I know less 15 lines of perl script will do it, but I'm wondering if
    there is another way.

    Any Help will br appreciated.
    Ray
     
    Ray Muforosky, Mar 9, 2005
    #1
    1. Advertising

  2. Ray Muforosky

    Guest

    I think this one-line will help you.

    cat a.txt | perl -npe 's/^(.*thr=)\d*(\d\d,.*)$/\1\2/ '
    kasj tgn=0507, thr=12, options=sched,
    safa tgn=0504, thr=22, options=sched,
    asda tgn=0513, thr=22, options=sched,
    asca tgn=055, thr=62, options=sched,
    asca tgn=0514, thr=42, options=sched,

    or

    cat a.txt | perl -npe 's/^(.*thr=)\d*(\d\d,.*)$/\1\2/ if 3..4 ; '
    kasj tgn=0507, thr=12, options=sched,
    safa tgn=0504, thr=22, options=sched,
    asda tgn=0513, thr=22, options=sched,
    asca tgn=055, thr=62, options=sched,
    asca tgn=0514, thr=42, options=sched,


    Here you can get some more perl one-liner example.

    http://sfg.homeunix.com/support/viewtopic.php?t=47

    -SR
     
    , Mar 9, 2005
    #2
    1. Advertising

  3. Thanks for the help.Is there anyway I can execute the command without
    dumping the content of the file. Can do in file editing?
     
    Ray Muforosky, Mar 9, 2005
    #3
  4. Ray Muforosky <> wrote:


    > I need to do string substitution


    > asda tgn=0513, thr=122, options=sched,
    > asca tgn=055, thr=562, options=sched,


    > where there is "thr=###" I need make it "thr=##" deleting the first
    > digit.


    > asda tgn=0513, thr=22, options=sched,
    > asca tgn=055, thr=62, options=sched,



    > I know less 15 lines of perl script will do it, but I'm wondering if
    > there is another way.



    perl -pe 's/thr=\d(\d\d)/thr=$1/' filename


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 9, 2005
    #4
  5. Thanks for the help.Is there anyway I can execute the command without
    dumping the content of the file. Can do in file editing?
     
    Ray Muforosky, Mar 9, 2005
    #5
  6. Ray Muforosky

    Sun Guest

    you use use in-place editing option

    perl -i.old -npe 's/^(.*thr=)\d*(\d\d,.*)$/\1\2­/ if 3..4 ; ' a.txt

    -SR
     
    Sun, Mar 9, 2005
    #6
    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. Alun
    Replies:
    3
    Views:
    4,661
    Masudur
    Feb 18, 2008
  2. Prasad S
    Replies:
    2
    Views:
    260
    Dr John Stockton
    Aug 27, 2004
  3. Ron M.
    Replies:
    4
    Views:
    153
    Tad McClellan
    Jan 5, 2004
  4. Yogi
    Replies:
    2
    Views:
    147
    John W. Krahn
    Apr 9, 2008
  5. Joan Interactive Bussiness
    Replies:
    2
    Views:
    127
    Kyle T. Jones
    Feb 18, 2010
Loading...

Share This Page