-2

This question already has an answer here:

I want to capture all values from this xml file and print the values in file as out1.txt

remark - value from the xml mean the word in the double bracket

  more input.txt

 <app name="UAT/ECC/Global/MES/1206/MRP-S23"   ear="UAT/ECC/Global/MES/1206/MRP-S23.ear" xml="UAT/ECC/Glal/ME/120/MRP-  S23.xml"/>
 <app name="OQ/ediedbn/adSFSF/adSFSF-CL" ear="OQ/ebn/aSF/adSF- CL.ear"  xml="OQ/ediedbn/adSFSF/adSSF-CL.xml"/>
 <app name="OQ/ediedbn/adaEBS/adOrBS-HR-CL"  ear="OQ/ediedbn/adOraS/araEBS- HR-CL.ear" xml="OQ/eddbn/aOraEBS/adOEBS-   HR-CL.xml"/>
 <app name="UAT/CZ/LIMS/T068_01/LIMS-QA-S03" ear="UAT/CZ/LIS/T068_01/LIS-QA-     .ear" xml="UAT/CZ/LIMS/T068_01/LIMS-QA-S03.xml"/>

.

more out1.txt

UAT/ECC/Global/MES/1206/MRP-S23
UAT/ECC/Glal/ME/120/MRP-S23.xml
OQ/ediedbn/adSFSF/adSFSF-CL
OQ/ebn/aSF/adSF- CL.ear
.
.
.

please advice how to capture the values in the out1.txt file with awk / perl one liner , bash

marked as duplicate by jasonwryan, Mikel, grochmal, Michael Homer, countermode Jan 24 '17 at 8:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • XML often is restricted to a single root element, I'm not seeing that in your input.txt? Is it really XML? – thrig Jan 23 '17 at 20:32
  • 5
    using bash for general xml parsing is bad idea. You have so many different libraries in other languages, python for example, and many others suitable for this kind of scripting. You are going wrong path. – Boban P. Jan 23 '17 at 20:33
  • You want to catch values of fields name and xml or ear? Your last line of out1.txt has an ear value while the second line has an xml value – George Vasiliou Jan 23 '17 at 23:13
0

You can slice the input file using awk like this:

gv@debian:$ cat a.txt
<app name="UAT/ECC/Global/MES/1206/MRP-S23"   ear="UAT/ECC/Global/MES/1206/MRP-S23.ear" xml="UAT/ECC/Glal/ME/120/MRP-  S23.xml"/>
<app name="OQ/ediedbn/adSFSF/adSFSF-CL" ear="OQ/ebn/aSF/adSF- CL.ear"  xml="OQ/ediedbn/adSFSF/adSSF-CL.xml"/>
<app name="OQ/ediedbn/adaEBS/adOrBS-HR-CL"  ear="OQ/ediedbn/adOraS/araEBS- HR-CL.ear" xml="OQ/eddbn/aOraEBS/adOEBS-   HR-CL.xml"/>
<app name="UAT/CZ/LIMS/T068_01/LIMS-QA-S03" ear="UAT/CZ/LIS/T068_01/LIS-QA-     .ear" xml="UAT/CZ/LIMS/T068_01/LIMS-QA-S03.xml"/>

gv@debian:$ cat b.txt

gv@debian:$ awk -F"name=|ear=|xml=|/>" '{print $2} {print $4}' a.txt >b.txt

gv@debian:$ cat b.txt
"UAT/ECC/Global/MES/1206/MRP-S23"   
"UAT/ECC/Glal/ME/120/MRP-  S23.xml"
"OQ/ediedbn/adSFSF/adSFSF-CL" 
"OQ/ediedbn/adSFSF/adSSF-CL.xml"
"OQ/ediedbn/adaEBS/adOrBS-HR-CL"  
"OQ/eddbn/aOraEBS/adOEBS-   HR-CL.xml"
"UAT/CZ/LIMS/T068_01/LIMS-QA-S03" 
"UAT/CZ/LIMS/T068_01/LIMS-QA-S03.xml"

If you don't want to keep the double quotes you can semove them with sed like this:

gv@debian:$ sed -i 's/\"//g' b.txt
gv@debian:$ cat b.txt
UAT/ECC/Global/MES/1206/MRP-S23   
UAT/ECC/Glal/ME/120/MRP-  S23.xml
OQ/ediedbn/adSFSF/adSFSF-CL 
OQ/ediedbn/adSFSF/adSSF-CL.xml
OQ/ediedbn/adaEBS/adOrBS-HR-CL  
OQ/eddbn/aOraEBS/adOEBS-   HR-CL.xml
UAT/CZ/LIMS/T068_01/LIMS-QA-S03 
UAT/CZ/LIMS/T068_01/LIMS-QA-S03.xml

Or in one liner, pipe awk to sed :

gv@debian:$ awk -F"name=|ear=|xml=|/>" '{print $2} {print $4}' a.txt |sed 's/\"//g' >b.txt

Tip : If you want all the fields per input file line to be written in output file in one line then use {print $2 $4} (put the fields inside same brackets).

The key that this awk method works is that awk can accept multichar delimiters and also multiple delimiters separated by | (=or) .
awk delimiters are defined by option -F

If you need to save the ear values, replace {print $4} with {print $3}.

To get an idea about this awk slicing see all the fields as will be separated by awk:

$ awk -F"name=|ear=|xml=|/>" '{print "Field1="$1} {print "Field2="$2} {print "Field3="$3} {print "Field4="$4}' a.txt
Field1=<app 
Field2="UAT/ECC/Global/MES/1206/MRP-S23"   
Field3="UAT/ECC/Global/MES/1206/MRP-S23.ear" 
Field4="UAT/ECC/Glal/ME/120/MRP-  S23.xml"
Field1=<app 
Field2="OQ/ediedbn/adSFSF/adSFSF-CL" 
Field3="OQ/ebn/aSF/adSF- CL.ear"  
Field4="OQ/ediedbn/adSFSF/adSSF-CL.xml"
Field1=<app 
Field2="OQ/ediedbn/adaEBS/adOrBS-HR-CL"  
Field3="OQ/ediedbn/adOraS/araEBS- HR-CL.ear" 
Field4="OQ/eddbn/aOraEBS/adOEBS-   HR-CL.xml"
Field1=<app 
Field2="UAT/CZ/LIMS/T068_01/LIMS-QA-S03" 
Field3="UAT/CZ/LIS/T068_01/LIS-QA-     .ear" 
Field4="UAT/CZ/LIMS/T068_01/LIMS-QA-S03.xml"
0

I tried something like this to get what you want:

sed 's/[^\"]*\"\([^\"]*\)\"[^\"]*/\1\n/g' input.txt > out.txt

It searches for substrings within double quotes (but not having them inside) and gets all such substrings from each line in the input.txt file. It uses new line '\n' as a delimiter.

Not the answer you're looking for? Browse other questions tagged or ask your own question.