4

I have two sets of files test.csv data.xml.

I am trying to grep a specific field from test.csv and search the string in data.xml. If string is found then print the corresponding line in test.csv file.

Example

search string is field 3 server name

test.csv

111,xxx,serversugar,port90
222,yyy,servertorque,port190
333,aaa,serverastrix,port8080
422,yxy,servertorque,port290

data.xml

<group>
<hostname>servertorque</hostname>
<hostname>serverastrix</hostname></group>

Output expected

222,yyy,servertorque,port190
333,aaa,serverastrix,port8080
422,yxy,servertorque,port290
Zombo
  • 1
  • 62
  • 391
  • 407
Sudheej
  • 1,873
  • 6
  • 30
  • 57

3 Answers3

2

One way with awk

awk -v FS="[><,]" 'NR==FNR{a[$3]++;next}$3 in a' data.xml test.csv

Test:

$ cat data.xml
<group>
<hostname>servertorque</hostname>
<hostname>serverastrix</hostname></group>

$ cat test.csv
111,xxx,serversugar,port90
222,yyy,servertorque,port190
333,aaa,serverastrix,port8080
422,yxy,servertorque,port290

$ awk -v FS="[><,]" 'NR==FNR {a[$3]++;next} $3 in a' data.xml test.csv
222,yyy,servertorque,port190
333,aaa,serverastrix,port8080
422,yxy,servertorque,port290
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • Thanks but am actually trying to do the reverse, the original xml is mix of multiple nodes i want to extract field $3 from test.csv and for each item in this file at this position($3) i want to search data.xml if the string exists in the file then remove the line from orginal file test.csv – Sudheej Jun 11 '13 at 05:15
  • Hmm, if the string exists and you wish to remove the line from original test.csv file then how is your desired output showing them? – jaypal singh Jun 11 '13 at 05:19
  • am sorry it was supposed to be 'print the line' – Sudheej Jun 11 '13 at 05:21
  • What this does is it scans the xml file and if the node is present in test.csv it will print it. Isn't that the same? – jaypal singh Jun 11 '13 at 05:22
  • i want to do the reverse (scan test.csv file) and search in the xml file, the reason is data.xml file in reality is a superset of test.csv my requirement is to pick strings from test.csv and search them in data.xml – Sudheej Jun 11 '13 at 05:40
2

with GNU sed & grep in 2 steps

sed '/>\w\+</!d;s/.*>\(\w\+\).*/\1/' data.xml>pattern.txt
grep -wf pattern.txt test.csv

..output:

222,yyy,servertorque,port190
333,aaa,serverastrix,port8080
422,yxy,servertorque,port290
Community
  • 1
  • 1
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

I assume you need:

awk -F',' '$3==<string_you_need> { print $0 }' test.csv
Zombo
  • 1
  • 62
  • 391
  • 407
mishik
  • 9,973
  • 9
  • 45
  • 67
  • @Steven Thanks !! but the requirement is to get the search pattern as field value $3 from test.csv file. – Sudheej Jun 11 '13 at 05:19