我想使用 diff 或 soemthing else 比较两个文件。文件上的每一行都以"line_x"开头。文件:
line_1: This is line1
line_2: This is line2
....
我想比较没有line_x的文件。 像这样:
diff <(sed '/line/,/:/g'diff1) <(sed '/line/,/:/g' diff2)
但是文我打印的差异,我想插入不同的确切line_x。用awk或其他东西可以做到这一点吗?谢谢
您可以尝试以下操作:
awk -f cmp.awk file1.txt file2.txt
其中file1.txt
和file2.txt
是您的输入文件,cmp.awk
是
NR==FNR {
$1=""
b[FNR]=$0
next
}
{
$1=""
if ($0!=b[FNR]) {
printf "Line: %dn", FNR
printf " File 1: %sn", b[FNR]
printf " File 2: %sn", $0
}
}
如果两个文件中的行未排序,您可以尝试:
NR==FNR {
a=$1; $1=""
b[a]=$0
next
}
{
a=$1; $1=""
if ($0!=b[a]) {
printf "%sn", a
printf " File 1: %sn", b[a]
printf " File 2: %sn", $0
}
}