Quote:
Originally Posted by
gksenthilkumar
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/'