我有两组文件test.csv data.xml。
我正在尝试从test.csv中搜索特定字段并在data.xml中搜索字符串。如果找到string,则在test.csv文件中打印相应的行。
示例
搜索字符串是字段3服务器名称
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>
预期输出
222,yyy,servertorque,port190
333,aaa,serverastrix,port8080
422,yxy,servertorque,port290
答案 0 :(得分:2)
使用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
答案 1 :(得分:2)
sed '/>\w\+</!d;s/.*>\(\w\+\).*/\1/' data.xml>pattern.txt
grep -wf pattern.txt test.csv
..输出:
222,yyy,servertorque,port190 333,aaa,serverastrix,port8080 422,yxy,servertorque,port290
答案 2 :(得分:0)
我认为你需要:
awk -F',' '$3==<string_you_need> { print $0 }' test.csv