将多个标题与详细信息文件合并

将多个标题与详细信息文件合并

我需要将标题与详细记录连接起来。

注意:该文件中的标头记录多于 1 个,并且标头计数的数量是动态的。我需要获取以下输出文件 Merge.txt。

基于标题和详细信息中的最后一个字段,我必须将标题与在两个单独的文件中创建的详细记录合并。

输入:

标题.txt

20180731                        HD0000000000000000000000007AAAA
20180731                        HD0000000000000000000000003AAAB
20180731                        HD0000000000000000000000002AAAC
20180731                        HD0000000000000000000000004AAAD

详情.txt

20180731                                1AAAA
20180731                                2AAAA
20180731                                3AAAA
20180731                                4AAAA
20180731                                5AAAA
20180731                                6AAAA
20180731                                7AAAA
20180731                                1AAAB
20180731                                2AAAB
20180731                                3AAAB
20180731                                1AAAC
20180731                                2AAAC
20180731                                1AAAD
20180731                                2AAAD
20180731                                3AAAD
20180731                                4AAAD

输出:

合并.txt

20180731 HD0000000000000000000000007AAAA
20180731 1AAAA
20180731 2AAAA
20180731 3AAAA
20180731 4AAAA
20180731 5AAAA
20180731 6AAAA
20180731 7AAAA
20180731 HD0000000000000000000000003AAAB
20180731 1AAAB
20180731 2AAAB
20180731 3AAAB
20180731 HD0000000000000000000000002AAAC
20180731 1AAAC
20180731 2AAAC
20180731 HD0000000000000000000000004AAAD
20180731 1AAAD
20180731 2AAAD
20180731 3AAAD
20180731 4AAAD

答案1

尝试下面的代码,

rev Header.txt Detail.txt   | awk ' /DH/{$4=1}1' FS="" OFS="" | sort | awk ' /DH/{$4="A"}1' FS="" OFS="" | rev | column -t > Merge.txt

输出:

20180731  HD0000000000000000000000007AAAA
20180731  1AAAA
20180731  2AAAA
20180731  3AAAA
20180731  4AAAA
20180731  5AAAA
20180731  6AAAA
20180731  7AAAA
20180731  HD0000000000000000000000003AAAB
20180731  1AAAB
20180731  2AAAB
20180731  3AAAB
20180731  HD0000000000000000000000002AAAC
20180731  1AAAC
20180731  2AAAC
20180731  HD0000000000000000000000004AAAD
20180731  1AAAD
20180731  2AAAD
20180731  3AAAD
20180731  4AAAD

答案2

我注意到标头本身包含该组的结束条件。因此,我们读取标题行,然后开始打印详细信息行,直到最后一个字段与当前标题匹配。

awk '
    function getheader(  h) {
        getline h < "Header.txt"
        return h
    }
    BEGIN {want_header = 1}
    want_header {
        head = getheader()
        print head
        want_header = 0
    }
    {print}
    head ~ "0*" $NF "$" {want_header = 1}
' Detail.txt
20180731                        HD0000000000000000000000007AAAA
20180731                                1AAAA
20180731                                2AAAA
20180731                                3AAAA
20180731                                4AAAA
20180731                                5AAAA
20180731                                6AAAA
20180731                                7AAAA
20180731                        HD0000000000000000000000003AAAB
20180731                                1AAAB
20180731                                2AAAB
20180731                                3AAAB
20180731                        HD0000000000000000000000002AAAC
20180731                                1AAAC
20180731                                2AAAC
20180731                        HD0000000000000000000000004AAAD
20180731                                1AAAD
20180731                                2AAAD
20180731                                3AAAD
20180731                                4AAAD

相关内容