我正在比较两个文件并显示不相似的行, 当我保持
echo $(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2)
我能够获取这些值。
当我尝试通过传入 array 来执行相同操作时,出现“:command not found”错误
declare -a myarr=()
myarr=$("$(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2 )")
请帮助,在此先感谢。
最佳答案
删除引号和前导美元:
myarr=$("$(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2 )")
应该是
myarr=($(awk 'FNR==NR{f[$0]+=1; next} !($0 in f)' $file1 $file2))
检查:
顺便说一句,您的 awk 命令不会打印 file1 独有的行。可能 comm
命令就是您想要的,但请注意 comm
需要排序的输入:
comm -13 <(sort "${file1}") <(sort "${file2}")
关于linux - 如何在 shell 脚本中获取 awk 的输出到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52971294/