0

I have the following xml structure and want to extract the first two letters from tag CUSTOMER_ID and STORE_ID from a single command using grep. I can do it for single tag using the following:

grep -oP '(?<=< STORE_ID >).*(?=< /STORE_ID >)' filename.* | awk '{print substr($0,2,2)}'
<CUSTOMER_ID>12345678910</CUSTOMER_ID>
<FIRSTNAME>Shubham</FIRSTNAME>
<LASTNAME>Anand</LASTNAME>
<STORE_ID>mystore</STORE_ID>

How to do it for two or more tags?

Output should be in a single line. For example in in the above case the result should be 12 my

Note: I want the output from multiple files. So I need to put filename.*

1 Answer 1

0

Based on the sample input and output you provided, here is a simple sed solution for a single file infile:

$ cat infile
<CUSTOMER_ID>12345678910</CUSTOMER_ID>
<FIRSTNAME>Shubham</FIRSTNAME>
<LASTNAME>Anand</LASTNAME>
<STORE_ID>mystore</STORE_ID>

$ sed -n -e 's/<CUSTOMER_ID>\(..\)\(.*\)<.*>/\1/p' -e 's/<STORE_ID>\(..\)\(.*\)<.*>/\1/p' infile | sed '$!N;s/\n/ /'
12 my
$

You can easily handle multiple files by wrapping the above in a simple shell script loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .