Extract part of word from XML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract part of word from XML
# 1  
Old 01-25-2012
Extract part of word from XML

Hi All,

Can Someone help me in capturing a word from xml Using sed or awk or any other way in unix.

i have file abc.xml like this
Code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?> 
- <NREC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <HEADER>
  <SOURCE>MAIL</SOURCE> 
  <MSGID>7971</MSGID> 
  <MESSAGETYPE>NRECAB</MESSAGETYPE> 
  <MESSAGEFUNCTION>NEWM</MESSAGEFUNCTION> 
  <REVID>30842</REVID> 
  <MARKET>US</MARKET> 
  <PROCESSINGDESK>01</PROCESSINGDESK> 
  <PREPDATE>20120123012446</PREPDATE> 
  </HEADER>

when i ftp'ed this file to unix it became single line like
Code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><NREC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><HEADER><SOURCE>MAIL</SOURCE><MSGID>797</MSGID><MESSAGETYPE>NRECAB</MESSAGETYPE><MESSAGEFUNCTION>NEWM</MESSAGEFUNCTION><REVID>30842</REVID><MARKET>US</MARKET><PROCESSINGDESK>01</PROCESSINGDESK><PREPDATE>20120123012446</PREPDATE></HEADER>

Now my requirement is to extract only " NRECAB" and "US" .

Please help me capture this particular words single or in separate commands.

Thanks in Advance,
Naveen kumar c

Last edited by Franklin52; 01-25-2012 at 08:35 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-25-2012
Code:
$ perl -ne 'if(/<MESSAGETYPE|<MARKET/){s/.*?>(.*?)<.*/$1/; print}' abc.xml
NRECAB
US

# 3  
Old 01-25-2012
Quote:
Originally Posted by balajesuri
Code:
$ perl -ne 'if(/<MESSAGETYPE|<MARKET/){s/.*?>(.*?)<.*/$1/; print}' abc.xml
NRECAB
US


Sorry Balajesri ,This command is not working
# 4  
Old 01-25-2012
Code:
awk -F"[<> ]" '{for(i=1;i<=NF;i++){if($i~"MESSAGETYPE|MARKET"){if($(i+1)){print $(i+1)}}}}'  infile

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 01-25-2012
@naveenkumarc: Please provide more details. What happens when you give that perl one-liner?
# 6  
Old 01-25-2012
Quote:
Originally Posted by balajesuri
@naveenkumarc: Please provide more details. What happens when you give that perl one-liner?
@Balajesuri: $ perl -ne 'if(/<MESSAGETYPE|<MARKET/){s/.*?>(.*?)<.*/$1/; print}' abc.xml

$


--No output.--

---------- Post updated at 08:50 AM ---------- Previous update was at 08:47 AM ----------

Quote:
Originally Posted by ahamed101
Code:
awk -F"[<> ]" '{for(i=1;i<=NF;i++){if($i~"MESSAGETYPE|MARKET"){if($(i+1)){print $(i+1)}}}}'  infile

--ahamed
Thanks ahamed,
Its working , can u explain me please , how above command is working.?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

2. Shell Programming and Scripting

Extract a part of a filename containing a particular word

Hi All, Thanks in Advance Shell Script or Perl Script I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv... (7 Replies)
Discussion started by: aealexanderraj
7 Replies

3. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. Programming

Extract xml data and create word document using perl.

Hi, I have large xml data file.I need to extract node and some tags in the node and after I need to create word document. my XMl data is look like as below -<student> <number>24</number> <education>bachelor</bachelor> <specialization>computers</specialization> ... (3 Replies)
Discussion started by: veerubiji
3 Replies

6. Programming

extract xml data and create word document using perl.

hi, i have large xml file which contains students information, i need to extract student number and some address tags and create a word document for the extracted data. my data looking llike this <student> <number>24</number> <education>bachelors</education> ... (1 Reply)
Discussion started by: veerubiji
1 Replies

7. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

8. Shell Programming and Scripting

How to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. The name should be set to the date, which is a part of the following line of the xml file: <sceneID>C82_N32_A_SM_strip_008_R_2009-11-24T04:22:12.790028Z</sceneID> How can I separate this line, that the name will... (6 Replies)
Discussion started by: friend
6 Replies

9. Shell Programming and Scripting

how to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. My code looks like this: set name = `awk '/<generationTime>/,/<\/generationTime>/ p' $xml_name` the "name" is thus set to <generationTime>2004-12-01T08:23:50.000000</generationTime> How can I separate this line,... (3 Replies)
Discussion started by: friend
3 Replies

10. Shell Programming and Scripting

Extract Part of a "Word", using AWK or SED????

I have been lurking on this forum for some time now and appreciate Everyone's help. I need to find a way to get the SystemID from this XML file. The file is much larger than just this one line but I can grep and get this line Printed. But really just need the "systemid". <test123: prefintem... (9 Replies)
Discussion started by: elbombillo
9 Replies
Login or Register to Ask a Question