file - 比较文件并打印匹配和差异
问题描述
我正在尝试比较 2 个文件并打印如下输出:
F1:
a|b|c|d|e|f|g
q|w|e|r||f|
F2:
a|b|c|d|e|f|g
q|w|e|r|t|f|u
输出:
f1 - a|b|c|d|e|f|g - f2 - a|b|c|d|e|f|g - All columns are matching
f1 - q|w|e|r||f| - f2 - q|w|e|r|t|f|u - Column 5 and 7 are not matching
解决方案
以下正是所要求的......(注意“- All”之前的两个空格和不匹配的大小写中的一个空格,并且 f1 和 f2 是小写的)。如果我们想使用文件名而不是 f1 和 f2,可以使用 FILENAME 变量
#! /usr/bin/awk -f
BEGIN {
FS = "|"
split("", f1)
}
NR == FNR { # this is true only for the first file processed
f1[FNR] = $0
next
}
$0 == f1[FNR] { # with the second file, if lines are equal...
print "f1 - " f1[FNR] " - f2 - " $0 " - All columns are matching"
next
}
{ # if the lines are not equal, split and find the columns not equal
sz = split(f1[FNR], f)
if (NF > sz)
sz = NF
c = ""
for (i=1; i<=sz; ++i)
if (f[i] != $i) {
c = i++
break
}
for (; i<=sz; ++i)
if (f[i] != $i)
c = c " and " i
print "f1 - " f1[FNR] " - f2 - " $0 " - Column " c " are not matching"
}
推荐阅读
- pandas - 如何防止 pandas 将原始数据库日期格式转换为 pandas 日期格式
- python - 替代循环遍历熊猫数据框行以应用条件?
- ruby-on-rails - 如何在 Rails 模型中映射数据?
- json - 如何转换可以表示多种类的 JSON 字符串?
- javascript - 将 Autoform 包与 Meteor Blaze 一起使用,但表单不会提交。没有错误
- macos - 在 MAC 上使用 SPSS Statistics 的困难
- sql-server - 如何在 SSIS 目录中查看包定义?
- jupyter-notebook - 我想编写代码来检查电子邮件地址的用户输入是否包含 1 次出现的“@”和“。”。请帮我调试
- javascript - 如何在 Javascript 中的 Keycloak 中获取当前登录用户的密码?
- c++ - 必须打印到屏幕才能使程序正常工作