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

标签 linux shell awk

我正在比较两个文件并显示不相似的行, 当我保持

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/

相关文章:

linux - 如何在日志中发送错误,在 shell 脚本中添加日期?

linux - openssl 命令挂起

linux - 可以在解析列之前在 awk 中搜索/替换吗?

c++ - 构建 OpenCV (C++) 时出错

python - 向 DNS 查询时欺骗 src ip 地址

shell - 在带有包含路径的变量的 shell 脚本中使用 cd 命令在 Windows 10 主机的 CentOS virtualbox 环境中不起作用

python - 从双引号包含的csv文件格式配置单元数据加载

awk - 在 awk 中转换为 int

bash - AWK 的 END block 中是否定义了字段?

awk - 使用 awk 在列数不等的表中打印长度为 32 个字符的条目