Explanation of the command sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Explanation of the command sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml
# 1  
Old 08-10-2010
Explanation of the command sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml

Hi Friends,


I am trying to modify a script .The script contains this line:
Code:
sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml

I am not great in sed command.I know, it is regular expression
to match a pattern string that starts with s_ and ends with 1_.I doesnot give the desired result.
Can anybody help in understanding this command.

Thanks in advance,
raj.

Moderator's Comments:
Mod Comment Use code tags, ty.

Last edited by zaxxon; 08-10-2010 at 07:55 AM..
# 2  
Old 08-10-2010
Code:
sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml

  • Brown: Go through the file, but don't print anything by default
  • Red: The following regex is a substitution
  • Blue:Look for any text on a line leading up to a right angle bracket, and ending with any text after a left angle bracket
  • Green: Capture and save the text between the brackets
  • Orange: Replace anything that matches the blue part with the saved capture
  • Dark Blue: If something is matched and substituted, print the substituted line

Basically, it extracts the text between tags in the XML file, without changing it.
This User Gave Thanks to pludi For This Post:
# 3  
Old 08-10-2010
Absolutely Perfect...The reply was perfectly helpfull....

Hi pludi,



Thanks for quick reply, It was a great help.It separates the text from the .xml file.

But i wudlike to understand sed better.The normal syntax of sed for substitution is as follows:sed s/day/night/ <old >new
Here day is being substituted by night in the file old.


But sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml doesnot have similar syntax
I wud appreciate if you cud explain it a little more.
Why sed -n -e 's_.*>\(.*\)<.*_\1_p' doesnot follow the same syntax.


Thanks pludi ind advance

Moderator's Comments:
Mod Comment Moved from the AIX sub-forum - it's not an AIX-specific question

Last edited by Scott; 08-10-2010 at 03:03 PM.. Reason: Moved thread
# 4  
Old 08-10-2010
sed doesn't have to use / as the seperator, it can use most non-special characers. s/day/night/ could also be done as s#day#night#. This is especially convenient when doing things like paths that'd otherwise be a mess of escapes:

Code:
# bleh
sed "s/\/usr\/bin/\/usr\/sbin/" ...
# readable
sed "s#/usr/bin#/usr/sbin#" ...

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-10-2010
Quote:
Originally Posted by rajsharma
Hi pludi,



Thanks for quick reply, It was a great help.It separates the text from the .xml file.

But i wudlike to understand sed better.The normal syntax of sed for substitution is as follows:sed s/day/night/ <old >new
Here day is being substituted by night in the file old.


But sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml doesnot have similar syntax
I wud appreciate if you cud explain it a little more.
Why sed -n -e 's_.*>\(.*\)<.*_\1_p' doesnot follow the same syntax.


Thanks pludi ind advance

because this syntax has differ the meaning:

Code:
\( pattern \)

--> save to buffer (mem) matched string(s) or portions with our pattern
Code:
\1

--> we can use it when we want to print only this mathced string another not Smilie
This User Gave Thanks to ygemici For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command explanation

Will someone give me an explanation on how the sed command below works. sed 's/.*//' Thanks! (3 Replies)
Discussion started by: scj2012
3 Replies

2. Shell Programming and Scripting

awk and or sed command to sum the value in repeating tags in a XML

I have a XML in which <Amt Ccy="EUR">3.1</Amt> tag repeats. This is under another tag <Main>. I need to sum all the values of <Amt Ccy=""> (Ccy may vary) coming under <Main> using awk and or sed command. can some help? Sample looks like below <root> <Main> ... (6 Replies)
Discussion started by: bk_12345
6 Replies

3. UNIX for Dummies Questions & Answers

xml to csv using sed and awk command

Hi Guys, Can you help me in creating shell script using sed,awk etc commands to generate csv file using xml file. (5 Replies)
Discussion started by: sbk
5 Replies

4. Shell Programming and Scripting

sed sorting command explanation

sed '$!N; /^\(.*\)\n\1$/!P; D' i found this file which removes duplicates irrespective for sorted or unsorted file. keep first occurance and remove the further occurances. can any1 explain how this is working.. i need to remove duplicates following file. duplicate criteria is not the... (3 Replies)
Discussion started by: mukeshguliao
3 Replies

5. UNIX for Dummies Questions & Answers

SED command explanation

can someone please explain the below sed command.. sed 's/\(*|\)\(.*\)/\2\1/' (6 Replies)
Discussion started by: raghu_shekar
6 Replies

6. Shell Programming and Scripting

Explanation for interesting sed behaviour?

This is my first post so hi to you all. I have browsed these forums in the past and what a great community and resource this is! Thanks to all the contributors ... I look forward to being able to give something back. In the meantime, I have a little conundrum concerning sed. My very simple... (6 Replies)
Discussion started by: Gavster
6 Replies

7. Shell Programming and Scripting

read mp3 filename and create one XML for each file

Hi: I have a collection of mp3s and I need to create 1 xml file per mp3. I have: recording1.mp3 recording2.mp3 etc and I want to generate this kind of files. recording1.xml recording2.xml and inside each xml file I need to add a url prefix and then the filename at the end. ... (4 Replies)
Discussion started by: jason7
4 Replies

8. Shell Programming and Scripting

Sed command to clean xml tag

Hi, Can someone help me come up with a generic sed command to clean a tag off its attributes? For eg. Input String - <tag attrib=new>This String</tag> should undergo a sed transformation to get Output String - <tag >This String</tag> This works - echo "<tag attrib=new>This</tag>" |... (3 Replies)
Discussion started by: iamwha1am
3 Replies

9. Shell Programming and Scripting

A sed doubt - need explanation

Hi, The following command works fine for me, but I could not grasp the logic working behind of sed command, it's obscure to me :( :confused: echo "./20080916/core/audioex.amr" | sed "s%\(\)/%\1_%g" o/p: ./20080916_core_audioex.amr Could anyone please explain to me in detail, that how... (6 Replies)
Discussion started by: royalibrahim
6 Replies

10. Shell Programming and Scripting

sed command explanation needed

Hi, Could you please explain me the below statement -- phrase wise. sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt if suppose $cnt contains value: 10 it copies last 9 lines of abc.txt to xyz.txt why it is copying last 9 rather than 10. and also what is ba and $D over there in... (4 Replies)
Discussion started by: subbukns
4 Replies
Login or Register to Ask a Question