backreferencing in sed

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 15
Reputation: ymf is an unknown quantity at this point 
Solved Threads: 0
ymf ymf is offline Offline
Newbie Poster

backreferencing in sed

 
0
  #1
Jun 17th, 2008
Hi,

I'm going over this topic in unix and i'd be glad if someone could explain this bit of script:

sed 's/\(abc\)*/xyz/'
so the input would be abcabcabc
which results in xyz
Also how come 'xyz' doesnt repeat 3 times because the input is 'abc' x 3 ?

Thanks in advance,

ymf
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: backreferencing in sed

 
0
  #2
Jun 17th, 2008
sed 's/string1/string2/'
replaces string1 with string2. the 's' signifies the substitution.

you are grouping the letters 'abc' i.e. (abc) using \(abc\) using the \ to escape the literal interpretation of the parenthesis.

The asterisk is used to signify one or more occurences of the group 'abc'.

It is the reason you are getting a single 'xyz' as the output.

type the same without the asterisk to see the change in output.

Hope it helps!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 15
Reputation: ymf is an unknown quantity at this point 
Solved Threads: 0
ymf ymf is offline Offline
Newbie Poster

Re: backreferencing in sed

 
0
  #3
Jun 17th, 2008
Yes definitely it all makes sense now,

Thanks alot !
Last edited by ymf; Jun 17th, 2008 at 10:55 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC