0

I need to concatenate headers with detail records.

Note: Header records are more than 1 in that file and number of header counts is dynamic. I need to get below output file Merge.txt.

Based on the Last field in both header and detail, I have to merge the header with detail records which is created in two separated files.

Input:

Header.txt

20180731                        HD0000000000000000000000007AAAA
20180731                        HD0000000000000000000000003AAAB
20180731                        HD0000000000000000000000002AAAC
20180731                        HD0000000000000000000000004AAAD

Detail.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

Output:

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
  • How is the merge role? – αғsнιη Jul 31 '18 at 6:04
  • Could you please make your question clearer? Ask a clear question to get a clear answer. – Bob Jul 31 '18 at 6:04
  • Question is Based on Last field in both header and detail I have to merge header with detail records which is created in two separated files. – Chakkara Jul 31 '18 at 6:08
  • 1
    what creates the ordering in the output? – Jeff Schaller Jul 31 '18 at 11:09
1

Try the following code,

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

Output:

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
  • You should explain what your solution does, as many people are not familiar with awk and you've certainly got a lot going on there. – Centimane Aug 1 '18 at 16:25
0

I notice that the header itself contains the end condition for the group. So, we read a header line and then start printing details lines until the last field matches the current header.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.