The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-04-2007
Registered User
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 65
sed question

Hi, can anyone explain step by step what this line of sed code is trying to do. Thanks.

Sed -e "s/big \(.*\) dog/small \1 cat/" filename

Last edited by NycUnxer; 10-04-2007 at 09:04 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-04-2007
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Does this help?
Code:
echo "a big black dog" | sed -e 's/big \(.*\) dog/small \1 cat/'
a small black eared cat
The \1 means that the first pattern that matches (.*) (any word) is put in. Similarly, another (second) matching pattern can also be put in. As an example:
Code:
echo "a big black eared dog ran away" | sed -e 's/big \(.*\) dog \(.*\)/small \1 cat \2/'
a small black eared cat ran away
Reply With Quote
  #3 (permalink)  
Old 10-06-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,490
Quote:
Originally Posted by blowtorch View Post
Does this help?
Code:
echo "a big black dog" | sed -e 's/big \(.*\) dog/small \1 cat/'
a small black eared cat
The \1 means that the first pattern that matches (.*) (any word) is put in. Similarly, another (second) matching pattern can also be put in. As an example:
Code:
echo "a big black eared dog ran away" | sed -e 's/big \(.*\) dog \(.*\)/small \1 cat \2/'
a small black eared cat ran away
Torch,

this is the not the way it works
Code:
echo "a big black dog" | sed -e 's/big \(.*\) dog/small \1 cat/'
a small black cat
Shouldn't the echo statement be as,
Code:
echo "a big black eared dog"
Reply With Quote
  #4 (permalink)  
Old 10-10-2007
Registered User
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 65
Ok guys, thanks for the confusion lol. Hope someone can clarify for sure.
Reply With Quote
  #5 (permalink)  
Old 10-10-2007
Registered User
 

Join Date: Oct 2007
Posts: 34
Sed -e "s/big \(.*\) dog/small \1 cat/" filename

Explanations:

1> -e - is used when using multiple replacements usinf regular expressions or not
2> you are searching for "big" and "anything after that" and then "dog"
3> you are replacing "big" with "small" and "dog" with "cat" without deterioriating the "anything after that" text

when sed searches, it remembers the \(.*\) and it can be accessed using \1.

hope this helps.


Cheers,
Devaraj Takhellambam
Reply With Quote
  #6 (permalink)  
Old 10-10-2007
Registered User
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 65
Thanks Devtakh, that helped alot actually.
Reply With Quote
  #7 (permalink)  
Old 10-11-2007
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Sorry for the confusion. I copied the wrong output there.
Reply With Quote
Google UNIX.COM
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0