Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string substitution help (search and replace)

Reply
Thread Tools

string substitution help (search and replace)

 
 
Ray Muforosky
Guest
Posts: n/a
 
      03-09-2005
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

 
Reply With Quote
 
 
 
 
sfgroups@gmail.com
Guest
Posts: n/a
 
      03-09-2005

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

 
Reply With Quote
 
 
 
 
Ray Muforosky
Guest
Posts: n/a
 
      03-09-2005
Thanks for the help.Is there anyway I can execute the command without
dumping the content of the file. Can do in file editing?

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-09-2005
Ray Muforosky <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ray Muforosky
Guest
Posts: n/a
 
      03-09-2005
Thanks for the help.Is there anyway I can execute the command without
dumping the content of the file. Can do in file editing?

 
Reply With Quote
 
Sun
Guest
Posts: n/a
 
      03-09-2005
you use use in-place editing option

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

-SR

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gsub and slash-ampersand in the substitution string Jani Patokallio Ruby 2 10-09-2006 02:44 PM
Keyword substitution in string Ondrej Krajicek Python 3 08-12-2004 06:32 PM
String substitution -- reactions? sdd Python 1 11-03-2003 11:08 PM
Q: string substitution in a file Troll Perl 6 09-26-2003 01:50 PM
Begineer Question : Global string substitution with re peter leonard Python 0 09-22-2003 09:22 AM



Advertisments