0

Need to take inputs from a file,then search that set of content from a XML file.In below sample file need to take input (John,MANG,102) from serch_file.txt,then need to search that combination each record H> to /H>) if found need to take the ADR> to /ADR> content from that record(H> to /H>) and write into the output.txt file.

INPUT FILE

serch_file.txt

John,MANG,102

XML FILE

XML_file.xml

<H>
 <NAM>John</NAM>
 <DEG>DEV</DEG>
 <ID>100</ID>
 <ADR>
  HOME 1,USA
  Pin-12345
</ADR>
</H>
<H>
 <NAM>John</NAM>
 <DEG>ANALIST</DEG>
 <ID>101</ID>
 <ADR>
  HOME 3,USA
  Pin-12345
</ADR>
</H>
<H>
 <NAM>John</NAM>
 <DEG>MANG</DEG>
 <ID>102</ID>
 <ADR>
  HOME 2,UK
  Pin-54321
</ADR>
</H>

OUT PUT FILE

output.txt

John,MANG,102

HOME 2,UK

Pin-54321

My Work

IFS=,
while read name deg id
do
cat XML_file.xml
--here difficult to check that combination to each 3 <H> to </H> record
> output.txt
done < serch_file.txt
Munu
  • 103
  • 5
  • 15
  • Personally, I strongly recommend the XMLStarlet based answers on the (many) duplicate questions. – Charles Duffy Mar 21 '18 at 14:00
  • This is not duplicate,here i am taking values as input and checking each and every records,if match that combination taking the another field of that particular record of XML. – Munu Mar 21 '18 at 14:52
  • So what you want is (in XPath terms) `//H[NAM='John'][DEG='MANG'][ID=102]/ADR`. Pretty much every duplicate I linked tells you how to run XPath queries. – Charles Duffy Mar 21 '18 at 14:57
  • You could also run something like `rs=$'\x01'; fs=$'\x02'; while IFS="$fs" read -r -d "$rs" h nam deg id adr; do [[ $nam = $dest_nam && $deg = $dest_deg && $id = $dest_id ]] && printf '%s\n' "$nam,$deg,$id" "$adr"; done < <(xmlstarlet sel -t -m '//H' -v ./NAM -o "$fs" -v ./DEG -o "$fs" -v ./ID -o "$fs" -v ./ADR -o "$rs")`, or such (not tested, edit to taste, requires the values you're searching for to be in the `dest_*` variables). Again, all the necessary elements are taught in the flagged duplicates. – Charles Duffy Mar 21 '18 at 15:01
  • (...as for "without using any tool", see also said duplicates describing why that's a fool's errand; without tools that understand XML, you can't ignore comments; you can't treat CDATA sections as literal, your code doesn't understand when whitespace is or isn't significant, etc -- so you can't actually parse real XML, just an oversimplified subset. But if that's *really* what you want to do, there are approaches that do that in the duplicate list as well). – Charles Duffy Mar 21 '18 at 15:05

0 Answers0