以下AWK脚本 script.awk 也可以完成这项工作:
NR == FNR { lines[++i] = $0 }
NR > FNR { patterns[++j] = $0 }
END {
for (p_index in patterns)
for (l_index in lines)
if (index(lines[l_index], patterns[p_index]) > 0) {
lines_match[l_index] = 1
patterns_match[p_index] = 1
}
print "Lines only in first file:"
for (l_index in lines)
if (!(l_index in lines_match))
print lines[l_index]
print "Lines only in second file:"
for (p_index in patterns)
if (! (p_index in patterns_match))
print patterns[p_index]
print "Lines in both files:"
for (l_index in lines)
if (l_index in lines_match)
print lines[l_index]
}
它可以如下调用:
awk -f script.awk test1.txt test2.txt
请注意,脚本不会对两个文件中的数据结构做任何假设 . 它只是假设 test2.txt 中的行是 test1.txt 中行的潜在子串 .