Extract the specific tags in a XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the specific tags in a XML file
# 22  
Old 10-20-2017
Moderator's Comments:
Mod Comment Password hash anonymized in all posts.

Please anonymize relevant data before posting
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 23  
Old 10-20-2017
Quote:
Originally Posted by Scott
Why don't you just post the "non-one liner" to start with?
Hello Scott,

I have learnt by hard way and most of time(almost always) write one liner programs first. Then I will convert them into non-one liner forms and post it, reason is why I don't remove the one-liner form of solution because OP may be working on it.

Thanks,
R. Singh
# 24  
Old 10-25-2017
Hello R.Singh,

Is there a way to repeat the below mentioned code for multiple input files.
Code:
awk -F"[><]" '
/jdbc-data-source/{
  a=1
}
a && /<name>.*<\/name>/{
  print "DS_Name:"$3;
  a=""
  next
}
/<url>.*<\/url?/{
  print "URL:"$3;
  next
}
/<driver-name>.*<\/driver-name>/{
  print "Driver_Name:"$3
  next
}
/<value>.*<\/value>/{
  print "ENV_Name:"$3
  next
}
/<password-encrypted>.*<\/password-encrypted>/{
  print "Password:"$3
  next
}
/<jndi-name>.*<\/jndi-name>/{
  print "JNDI_Name:"$3
  next
}

Please consider that i have multiple files in one location like below.
For EX:-
1) aqjmsuserDataSource-1696-jdbc.xml
2) aqSample-8170-jdbc.xml
3) DataSourceTest-8855-jdbc.xml

Now, i would like to run the above mentioned script for each file and need to create a separate output file.

the expected output needs to be like below

1) aqjmsuserDataSource-1696-jdbc.properties
2) aqSample-8170-jdbc.properties
3) DataSourceTest-8855-jdbc.properties

Any suggestions please?

Thanks,
Siva
# 25  
Old 10-25-2017
Hello Siv51427882,

Could you please try following and let me know if this helps(Not tested it though).
Code:
awk -F"[><]" '
FNR==1{sub(/\..*/,"",FILENAME);file=FILENAME".properties"}
/jdbc-data-source/{
  a=1
}
a && /<name>.*<\/name>/{
  print "DS_Name:"$3; > file
  a=""
  next
}
/<url>.*<\/url?/{
  print "URL:"$3;     > file
  next
}
/<driver-name>.*<\/driver-name>/{
  print "Driver_Name:"$3 > file
  next
}
/<value>.*<\/value>/{
  print "ENV_Name:"$3 > file
  next
}
/<password-encrypted>.*<\/password-encrypted>/{
  print "Password:"$3 > file
  next
}
/<jndi-name>.*<\/jndi-name>/{
  print "JNDI_Name:"$3 > file
  next
}'  aqjmsuserDataSource-1696-jdbc.xml   aqSample-8170-jdbc.xml   DataSourceTest-8855-jdbc.xml

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 26  
Old 10-25-2017
Hello R.Singh,

Getting syntax errors like below.

awk: cmd. line:6: print "DS_Name:"$3; > file
awk: cmd. line:6: ^ syntax error
awk: cmd. line:11: print "URL:"$3; > file
awk: cmd. line:11: ^ syntax error


Thanks
Siva
# 27  
Old 10-25-2017
Hello Siv51427882,

Sorry I forgot to remove ; in above, try following and let me know if this helps you.
Code:
awk -F"[><]" '
FNR==1{sub(/\..*/,"",FILENAME);file=FILENAME".properties"}
/jdbc-data-source/{
  a=1
}
a && /<name>.*<\/name>/{
  print "DS_Name:"$3 > file
  a=""
  next
}
/<url>.*<\/url?/{
  print "URL:"$3     > file
  next
}
/<driver-name>.*<\/driver-name>/{
  print "Driver_Name:"$3 > file
  next
}
/<value>.*<\/value>/{
  print "ENV_Name:"$3 > file
  next
}
/<password-encrypted>.*<\/password-encrypted>/{
  print "Password:"$3 > file
  next
}
/<jndi-name>.*<\/jndi-name>/{
  print "JNDI_Name:"$3 > file
  next
}'  aqjmsuserDataSource-1696-jdbc.xml   aqSample-8170-jdbc.xml   DataSourceTest-8855-jdbc.xml

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 28  
Old 10-25-2017
Thank You R.Singh.. That worked like a Gem...!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

How can we extract specific elements from XML?

Hi, I have a requirement to extract specific element value dynamically from XML message. Here is the sample message: <File> <List> <main> <dir>doc/store834/archive</dir> <count>5</count> </main> <main> <dir>doc/store834/extract</dir> <count>6</count> </main> <main> ... (3 Replies)
Discussion started by: renukeswar
3 Replies

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

4. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

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

6. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

7. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

8. Shell Programming and Scripting

bash extract all occurences delimited from <name> and </name> tags from an xml file

I need to extract all text delimited from <name> and </name> tags from an xml file, but not only first occurence. I need to extract all occurences. I've tried with this command: awk -F"<name>|</name>" 'NF>2{print $2}' but it give only first occurence. How can i modify it? (18 Replies)
Discussion started by: ingalex
18 Replies

9. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

10. Shell Programming and Scripting

extract specific data from xml format file.

Hi, I need to extract the start time value (bold, red font) under the '<LogEvent ID="Timer Start">' tag (black bold) from a file with the following pattern. There are other LogEventIDs listed in the file as well, making it harder for me to extract out the specific start time that I need. . .... (7 Replies)
Discussion started by: 60doses
7 Replies
Login or Register to Ask a Question