The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2007
NycUnxer NycUnxer is offline
Registered User
  
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 88
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 12:04 PM..
  #2 (permalink)  
Old 10-05-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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
  #3 (permalink)  
Old 10-06-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,932
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"
  #4 (permalink)  
Old 10-10-2007
NycUnxer NycUnxer is offline
Registered User
  
 

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

Join Date: Oct 2007
Location: Bangalore
Posts: 514
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
  #6 (permalink)  
Old 10-10-2007
NycUnxer NycUnxer is offline
Registered User
  
 

Join Date: Sep 2007
Location: AIX-VILLE
Posts: 88
Thanks Devtakh, that helped alot actually.
  #7 (permalink)  
Old 10-11-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Sorry for the confusion. I copied the wrong output there.
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On



All times are GMT -4. The time now is 04:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0