-2

Need sample gawk to join two records with unique key $2.$1 has different entity eventhough $2 is key thats why there are ther are two different lines for same key

I/p FIle not in sorted order

01|12345|AAAA 01|99|AAAA 01|888|AAAA 02|12345|BBBBB|CCCCC|DDDDD 02|99|BBBBB|CCCCC|DDDDD 02|888|BBBBB|CCCCC|DDDDD

Output file:

O/p file 01|12345|DDDDD 01|99|DDDDD 01|888||DDDDD

dileep
  • 9
  • 1
    This is quite unclear. If the key is unique, why are there multiple records with that key? What exactly is the transformation rule you want to apply? – Anko Apr 08 '15 at 21:16

1 Answers1

1

You can do that with awk, e.g. like this:

awk '
    BEGIN { FS=OFS="|" }
    $2!=old { print sav ; sav=$0 ; old=$2 ; next }
    { sub(/[^|]*\|[^|]*\|/,"") ; sav=sav OFS $0 }
    END { print sav }
'
Janis
  • 14,222
  • Thanks this help, added to the first request if the input is not sorted and expecting the o/p in diff way I/p FIle 01|12345|AAAA 01|99|AAAA 01|888|AAAA 02|12345|BBBBB|CCCCC|DDDDD 02|99|BBBBB|CCCCC|DDDDD 02|888|BBBBB|CCCCC|DDDDD

    O/p file 01|12345|DDDDD 01|99|DDDDD 01|888|DDDDD

    – dileep Apr 09 '15 at 18:53