我正在尝试比较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
答案 0 :(得分:0)
以下内容完全按照要求执行...(在“-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"
}