The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
doubt in tr command risshanth UNIX for Dummies Questions & Answers 2 02-25-2008 07:20 AM
doubt in AWK abnirmal Shell Programming and Scripting 4 05-08-2007 02:27 PM
Pls help for the doubt ravi.sadani19 Shell Programming and Scripting 4 04-12-2007 01:51 AM
doubt in cal command sumi AIX 9 03-03-2006 09:33 AM
Doubt in find command mona Shell Programming and Scripting 3 12-11-2005 09:54 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-19-2008
Registered User
 

Join Date: Feb 2006
Posts: 4
Doubt in SED command

PLEASE EXPLANIN ME...
sed 's~\(.*\)\(<name>\)\(.*\)\(</name>\)\(.*\)~\2\3\4~'

this is the format
<start><name>123<\name><addr>BAC<\addr><loc>sfo<\loc></start>
Reply With Quote
Forum Sponsor
  #2  
Old 02-19-2008
Registered User
 

Join Date: Jan 2007
Posts: 366
Quote:
Originally Posted by gksenthilkumar View Post
PLEASE EXPLANIN ME...
sed 's~\(.*\)\(<name>\)\(.*\)\(</name>\)\(.*\)~\2\3\4~'

this is the format
<start><name>123<\name><addr>BAC<\addr><loc>sfo<\loc></start>
.* zero or more random characters
<name> literally the string
</name> literally the string

Find lines with "<name>" and "</name>", where "<name>" can preceded by any amount of characters, "</name>" can be followed by any amount of characters, and there can be any amount of characters in between "<name>" and "</name>"

\(.....\) whatever is found put in a buffer, subsequent \(.....\) means put in next buffer.

1st \(.*\) will match "<start>" and this string will be placed in buffer 1
\(<name>\) will match "<name>" and this string will be placed in buffer 2
2nd \(.*\) will match "123" and this string will be placed in buffer 3
\(</name>\) will match "</name>" and this string will be placed in buffer 4
\(.*\) will match "<addr>BAC<\addr><loc>sfo<\loc></start>" and this string will be placed in buffer 5.

"~" is used in the sed instead of "/" so the "/" in "</name>" doesnt need to be escaped.

If a line is found which matches the patterns, replace them by what ever is in buffer 2,3 and 4.

So based on the string given:
<start><name>123<\name><addr>BAC<\addr><loc>sfo<\loc></start>

The result of the sed command:
sed 's~\(.*\)\(<name>\)\(.*\)\(</name>\)\(.*\)~\2\3\4~'

will be:
<name>123<\name>

Shorter:
sed 's~.*\(<name>\)\(.*\)\(</name>\).*~\1\2\3~'

With "/" instead of "~":
sed 's/.*\(<name>\)\(.*\)\(<\/name>\).*/\1\2\3/'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:05 AM.


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

Content Relevant URLs by vBSEO 3.2.0