【问题标题】:Shell comparing files [duplicate]Shell比较文件[重复]
【发布时间】:2015-06-30 08:58:56
【问题描述】:

这是我在名为 file.txt 的文件中的基线值:

us-east-1a m1.small 1
us-east-1c m1.small 1
us-east-1c m3.medium 1
us-east-1c m3.medium 1
us-east-1c t1.micro 1
us-east-1d m1.large 1
us-east-1d m1.medium 1
us-east-1e m3.medium 6

这是生成的新文件file2.txt

us-east-1a c3.large 1
us-east-1a m1.small 7
us-east-1a m3.medium 6
us-east-1c m1.small 1
us-east-1c m3.medium 6
us-east-1c t1.micro 1
us-east-1d m1.large 1
us-east-1d m1.medium 1
us-east-1d m3.large 1
us-east-1d t2.medium 1
us-east-1e m3.medium 23

我正在重新构建我想要的输出。我可以得到如下的确切输出吗?

us-east-1a c3.large 1
us-east-1a m3.medium 6
us-east-1d m3.large 1
us-east-1d t2.medium 1

【问题讨论】:

  • 是的,这是可能的。
  • 嘿马丁谢谢你能帮我提供示例脚本吗?
  • 我尝试使用 temp=0; while read line do echo $line > temp1.txt while read line2 do echo $line2 > temp2.​​txt diff temp1.txt temp2.​​txt > temp3.txt compare=cat temp3.txt | wc -l | head -1 if [ "$compare" -eq 0 ];然后温度=1; fi 完成

标签: shell command-line diff text-processing


【解决方案1】:

如果我理解正确,您希望比较前两个字段,如果它们不同,则应打印该行:

awk 'FNR==NR{a[$1$2];next}!($1$2 in a)' file.txt file2.txt

第一个文件file.txt 被读取到一个数组中。并且在处理第二个文件时比较数组内容。

【讨论】:

  • 感谢混沌。这是我所期望的
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
相关资源
最近更新 更多