Unix Technical Forum

string substitution help (search and replace)

This is a discussion on string substitution help (search and replace) within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hello everyone I need help. I need to do string substitution on over 350 files on the following string. ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 06:03 AM
Ray Muforosky
 
Posts: n/a
Default string substitution help (search and replace)

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 06:03 AM
sfgroups@gmail.com
 
Posts: n/a
Default Re: string substitution help (search and replace)


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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 06:03 AM
Ray Muforosky
 
Posts: n/a
Default Re: string substitution help (search and replace)

Thanks for the help.Is there anyway I can execute the command without
dumping the content of the file. Can do in file editing?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 06:03 AM
Tad McClellan
 
Posts: n/a
Default Re: string substitution help (search and replace)

Ray Muforosky <izombe@yahoo.com> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-05-2008, 06:03 AM
Ray Muforosky
 
Posts: n/a
Default Re: string substitution help (search and replace)

Thanks for the help.Is there anyway I can execute the command without
dumping the content of the file. Can do in file editing?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-05-2008, 06:03 AM
Sun
 
Posts: n/a
Default Re: string substitution help (search and replace)

you use use in-place editing option

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

-SR

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-05-2008, 06:03 AM
Laurenz Albe
 
Posts: n/a
Default Re: string substitution help (search and replace)

In comp.unix.aix Ray Muforosky <izombe@yahoo.com> wrote:
> 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.


I am sorry, I know no tricky command, but if simple commands will do:

In vi:

:%s/thr=[0-9]*\([0-9][0-9]\),/thr=\1,/

In sed:

sed -e 's/thr=[0-9]*\([0-9][0-9]\),/thr=\1,/'

This assumes that the number behind thr= has at least 2 digits.

Yours,
Laurenz Albe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-05-2008, 06:04 AM
Ray Muforosky
 
Posts: n/a
Default Re: string substitution help (search and replace)

Thanks everyone.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:27 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com