Sponsored Content
Top Forums Shell Programming and Scripting Extract Matched Records from XML Post 302962533 by Don Cragun on Tuesday 15th of December 2015 02:32:02 PM
Old 12-15-2015
Quote:
Originally Posted by looney
Hello Mr Don Thanks for this useful code. But could you please help me with some doubts.
Code:
(1)/<FOLDER / {
	nf = 0
	folder = $0
	next
}
(2)/<JOB / && $6 in jn {
	copy = 1
	if(!nf++)
		print folder
}
copy
(3)/<\/JOB>/ {
	copy = 0
	next
}
(4)/<\/FOLDER/ && nf {
	print
}' file1 file2

1. Is "copy" variable, Part of block (2) ?, which will have a value 1 .That will print as default.
In awk, the blocks you are talking about have the format:
Code:
    pattern { action }

and as you know if the { action } part is missing, it defaults to:
Code:
    pattern { print $0 }

So, the line:
Code:
copy

is a block of its own that could be rewritten as:
Code:
copy {	print
}

(since print with no arguments defaults to print $0). When the variable copy is set to a non-zero, non-empty value, that line causes the current line to be printed. And when copy is an empty string (the default value until it is set to something else) or zero that block is a no-op.
Quote:
2. What is the purpose of block (3) . As no print statement or default 1 is present.
Block (3) turns off copying input lines to the output after the end of a set of <JOB> tags:
Code:
<JOB...>
...
</JOB>

has been conditionally printed.
Quote:
3.Which part of code is printing other lines like <VARIABLE....<INCOND......
Thanks,
The line in the code:
Code:
copy

prints everything this script prints except the opening <FOLDER...> tag line that is printed in block (2) the 1st time a job is found that is to be printed in that folder and the </FOLDER> line that is printed in block (4) iff one or more jobs were found to be printed in the preceding folder.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Grep matched records from huge file

111111111100000000001111111111 123232323200000010001114545454 232435424200000000001232131212 342354234301000000002323423443 232435424200000000001232131212 2390898994200000000001238908092 This is the record format. From 11th position to 20th position in a record there are 0's occuring,and... (6 Replies)
Discussion started by: mjkreddy
6 Replies

3. Shell Programming and Scripting

Extract records from list

Hi Gents, I have a file 1 like this 1 1000 20 2 2000 30 3 1000 40 5 1000 50 And I have other file 1 like 2 1 I would like to get from the file 1 the complete line which are in file 2, the key to compare is the column 2 then output should be. 2 2000 30. I was trying to get it... (5 Replies)
Discussion started by: jiam912
5 Replies

4. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

5. Shell Programming and Scripting

Parse xml in shell script and extract records with specific condition

Hi 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"> <author>mantest</author> <date>2015-02-27</date> <QC_ID>334566</QC_ID>... (12 Replies)
Discussion started by: madankumar.t@hp
12 Replies

6. Shell Programming and Scripting

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

7. Shell Programming and Scripting

Extract all the sentences that matched two patterns

Hi I have two lists of patterns named A and B consisting of around 200 entries in each and I want to extract all the sentences from a big text file which match atleast one pattern from both A and B. For example, pattern list A consists of : ama ani ahum mari ... ... and pattern... (1 Reply)
Discussion started by: my_Perl
1 Replies

8. Shell Programming and Scripting

How to fetch matched records from files between two different directory?

awk 'NR==FNR{arr;next} $0 in arr' /tmp/Data_mismatch.sh /prd/HK/ACCTCARD_20160115.txt edit by bakunin: seems that one CODE-tag got lost somewhere. i corrected that, but please check your posts more carefully. Thank you. (5 Replies)
Discussion started by: suresh_target
5 Replies

9. Shell Programming and Scripting

Extract between two Exact matched strings.

more data.txt i need this exacted from data.txt This is the command i tried sed -n "/Start_of_DISK_info:\/u/,/End_of_DISK_info:\/u/p" data.txtBut, unfortunately it does not do an exact match. Instead, it prints text between both these strings /u & /u/tmp like below. i need this... (6 Replies)
Discussion started by: mohtashims
6 Replies

10. UNIX for Beginners Questions & Answers

Extract XML block when value is matched (Shell script)

Hi everyone, So i'm struggling with an xml (log file) where we get information about some devices, so the logfile is filled with multiple "blocks" like that. Based on the <devId> i want to extract this part of the xml file. If possible I want it to have an script for this, cause we'll use... (5 Replies)
Discussion started by: Pouky
5 Replies
All times are GMT -4. The time now is 01:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy