比较两个带分隔符的文本文件的 headers

Compare headers of two delimited text files

File1.txt(基本文件)

header1|header2|header3|header4

1|2|3|4

File2.txt

header1|header10|header3|header4

5|6|7

想要O/P

header2 is missing in file 2 at position 2

header10 is addition in file 2 at position 2

我需要比较两个文件 header 并且需要显示缺少的 header 或关于基本文件 header 列表的附加列。

我会像这样使用 diff 命令来尝试:

diff <(head -n1 fh1.txt | tr "|" "\n") <( head -n1 fh2.txt | tr "|" "\n") 

其中 fh1.txt 和 fh2.txt 是您的文件。输出提供了您想要的信息,但并不那么冗长。

您可以使用 awk,像这样:

check.awk

# In the first line of every input file save the headers
FNR==1{
    headers[f++]=[=10=]
}

# 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