File1.txt(基本文件)
header1|header2|header3|header4
1|2|3|4
FILE2.TXT
header1|header10|header3|header4
5|6|7
期望的O / P
在位置2的文件2中缺少header2
header10在位置2的文件2中添加
我需要比较两个文件头,并且需要显示相对于基本文件头列表的缺少的标题或添加列。
答案 0 :(得分:2)
您可以使用awk
,如下所示:
check.awk
# In the first line of every input file save the headers
FNR==1{
headers[f++]=$0
}
# Once all lines of input have been processed ...
END{
# split() returns the number of items. The resulting
# arrays 'a|b_headers' will be indexed starting from 1
lena = split(headers[0],a_headers,"|")
lenb = split(headers[1],b_headers,"|")
for(h=1;h<=lena;h++) {
if(a_headers[h] != b_headers[h]) {
print a_headers[h] " missing from file2 at column " h
}
}
for(h=1;h<=lenb;h++) {
if(b_headers[h] != a_headers[h]) {
print b_headers[h] " missing from file1 at column " h
}
}
}
这样称呼:
awk -f check.awk File1.txt File2.txt
输出:
header2 missing from file2 at column 2
header10 missing from file1 at column 2
答案 1 :(得分:1)
我会尝试diff
这样的命令:
diff <(head -n1 fh1.txt | tr "|" "\n") <( head -n1 fh2.txt | tr "|" "\n")
其中fh1.txt和fh2.txt是您的文件。输出提供了您想要的信息,但不是那么详细。