Parse xml in shell script and extract records with specific condition

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse xml in shell script and extract records with specific condition
# 1  
Old 04-01-2015
Parse xml in shell script and extract records with specific condition


I have xml file with multiple records and would like to extract records from xml with specific condition if specific tag is present extract entire row otherwise skip .

<logentry revision="21510">
<Description>SYNC-Stop Sync </Description>
<logentry revision="21511">
<author>poo test</author>
<Description> Update acquireRetryAttempt value </Description>
<Deployment_Change_Needed> Yes</Deployment_Change_Needed>
<Deployment_Change_Description> Update  Appconfig</Deployment_Change_Description>

From above when there QC_ID is null would like to extract entire record otherwise skip.

from the above sample input we should be able to see only second record as output .

any help appreciated
# 2  
Old 04-01-2015
Hello Madankumar.t@hp,

Following may help you in same.
awk '/^<logentry revision=/{A=1} A{O=O?O ORS $0:$0} /<QC_ID><\/QC_ID>/{print O;B=1;next} B{print $0} /<\/logentry>/{B=0;O=""}'  Input_file

R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-01-2015
Can you pl. explain the logic used.
# 4  
Old 04-01-2015
Hello Lakshmikumari,

Following is the explaination for same, hope this helps.
awk '/^<logentry revision=/{A=1}        ###### Search for text <logentry revision= then set a variable named A to 1 #####
     A{O=O?O ORS $0:$0}          ###### If A's value is 1 then I am creating a variable named O who will append the value of itself
     /<QC_ID><\/QC_ID>/{print O;B=1;next}  ###### Search text <QC_ID><\/QC_ID> it means looking for NULL values in <QC_ID></QC_ID> string and then printing the variable value of variable of B which are previous lines and then setting variable B to 1 and escaping next statements
     B{print $0}     ###### If B is TRUE means B's value is 1 then printing the line
      /<\/logentry>/{B=0;O=""}'   ###### When line has text </logentry> then made variable B's value to 0 and made O's value to 0 so that it shouldn';t print lines.

R. Singh
These 3 Users Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 04-01-2015
Try also
sed '/<\/logentry>/a
' file | awk '/<QC_ID><\/QC_ID>/' RS=

Please be aware that there MUST be a newline char after the a(ppend) command in sed!
This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-02-2015
Hi Ravindersingh

Thank you for your response , i tried to execute the same line with inputfile (copy pasted your code) , No errors found , but i have not seen output.

further assistance appreciated

is it something i am missing . Smilie


Last edited by madankumar.t@hp; 04-02-2015 at 05:43 AM.. Reason: spelling mistake
# 7  
Old 04-02-2015
Hello Madan,

You should write actual filename on place of Input_file and try the command.
awk '/^<logentry revision=/{A=1} A{O=O?O ORS $0:$0} /<QC_ID><\/QC_ID>/{print O;B=1;next} B{print $0} /<\/logentry>/{B=0;O=""}'  Actual_FILE_NAME

R. Singh
This User Gave Thanks to RavinderSingh13 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

Extract Matched Records from XML

Hi All, I have a requirement to extract para in XML file on the basis of another list file having specific parameters. I will extract these para from XML and import in one scheduler tool. file2 <FOLDER DATACENTER="ControlMserver" VERSION="800" PLATFORM="UNIX" FOLDER_NAME="SH_AP_INT_B01"... (3 Replies)
Discussion started by: looney
3 Replies

2. Shell Programming and Scripting

Extract top 20 records from sybase db in shell script

Hi, I am new to advanced scripting. I need to get top 20 records from database (Sybase) with a condition that all Char/varchar columns should have more than 4 characters. I need to do this via shell scripting, I have got half logic (on how to get varchar/char columns and stored it in a... (2 Replies)
Discussion started by: Selva_2507
2 Replies

3. Shell Programming and Scripting

How to Parse the XML data along with the URL in Shell Script?

Hi, Can anybody help to solve this. I want to parse some xmldata along with the URL in the Shell. I'm calling the URL via the curl command Given below is my shell script file export... (7 Replies)
Discussion started by: Megala
7 Replies

4. Shell Programming and Scripting

Extract and parse XML data (statistic value) to csv

Hi All, I need to parse some statistic data from the "measInfo" -eg. 25250000 (as highlighted) and return the result into line by line, and erasing all other unnecessary info/tag. Thought of starting with grep "measInfoID="25250000" but this only returns 1 line. How do I get all the output... (8 Replies)
Discussion started by: jackma
8 Replies

5. Shell Programming and Scripting

Shell script (not Perl) to parse xml with awk

Hi, I have to make an script according to these: - I have couples of files like: xxxxxxxxxxxxx.csv xxxxxxxxxxxxx_desc.xml - every xml file has diferent fields, but keeps this format: ........ <defaultName>2011-02-25T16:43:43.582Z</defaultName> ........... (2 Replies)
Discussion started by: Pluff
2 Replies

6. Shell Programming and Scripting

extract xml tag based on condition

Hi All, I have a large xml file of invoices. The file looks like below: <INVOICES> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>1234</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME> <INVOICE_NO>2345</INVOICE_NO> </INVOICE> <INVOICE> <NAME>Customer A</NAME>... (9 Replies)
Discussion started by: angshuman
9 Replies

7. Shell Programming and Scripting

Parse XML file in shell script

Hi Everybody, I have an XML file containing some data and i want to extract it, but the specific issue in my file is that the data is repeated some times like the following example : <section1> <subsection1> X=... Y=... Z=... <\subsection1> <subsection2> X=... Y=... Z=...... (2 Replies)
Discussion started by: yassine
2 Replies

8. Shell Programming and Scripting

regex/shell script to Parse through XML Records

Hi All, I have been working on something that doesn't seem to have a clear regex solution and I just wanted to run it by everyone to see if I could get some insight into the method of solving this problem. I have a flat text file that contains billing records for users, however the records... (5 Replies)
Discussion started by: Jerrad
5 Replies

9. Shell Programming and Scripting

Help with shell script to extract data from XML file

Hello Scripting Gurus, I need help with extracting data from the XML file using shell script. The data is in a large XML and I need to extract the id values of all completedworkflows. Here is a sample of it. Input and output data is also in the attached text files. <wfregistry>... (5 Replies)
Discussion started by: yajaykumar
5 Replies

10. Shell Programming and Scripting

Parse a string in XML file using shell script

Hi! I'm just new here and don't know much about shell scripting. I just want to ask for help in creating a shell script that will parse a string or value of the status in the xml file. Please sample xml file below. Can you please help me create a simple script to get the value of status? Also it... (46 Replies)
Discussion started by: ayhanne
46 Replies
Login or Register to Ask a Question

Featured Tech Videos