简体   繁体   English

如何在shell脚本中将awk的输出获取到数组中

[英]how to get output of awk into array in shell script

I am comparing two files and displaying the lines which are not similar, when I just keep 我只是比较两个文件,并显示不相似的行

echo $(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2)

I am able to get the values. 我能够得到这些价值。

When I try to do the same by passing into array , I am getting ":command not found" error 当我尝试通过传递给array来执行相同操作时,出现“:command not found”错误

declare -a myarr=()
myarr=$("$(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2 )")

Please help , thanks in advance. 请帮助,在此先感谢。

Remove the quotes plus the leading dollar: 删除报价加上前导美元:

myarr=$("$(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2 )")

should be 应该

myarr=($(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2))

Check: 校验:


Btw, your awk command won't print lines which are unique to file1. 顺便说一句,您的awk命令不会打印file1独有的行。 Probably the comm command is what you want, but note that comm expects sorted input: 可能是您想要的comm命令,但是请注意comm需要排序的输入:

comm -13 <(sort "${file1}") <(sort "${file2}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM