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
One way with awk
awk -v FS="[><,]" 'NR==FNR{a[$3]++;next}$3 in a' data.xml test.csv
$ 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
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
我假设你需要:
awk -F',' '$3==<string_you_need> { print $0 }' test.csv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.