BlueHam
BlueHam

Reputation: 37

awk append lines if certain field matches

I'm an absolute beginner to awk and would like some help with this.

I have this data:

FOO|BAR|1234|A|B|C|D|
FOO|BAR|1234|E|F|G|H|
FOO|BAR|5678|I|J|K|L|
FOO|BAR|5678|M|N|O|P|
FOO|BAR|5678|Q|R|S|T|

Desired output:

FOO|BAR|1234|A|B|C|D|E|F|G|H|
FOO|BAR|5678|I|J|K|L|M|N|O|P|Q|R|S|T|

Basically I have to append some fields to the lines where column 3 matches.

Appreciate any responses, thanks a lot!

Upvotes: 0

Views: 725

Answers (2)

gboffi
gboffi

Reputation: 25023

$ awk -f chain.awk < data
FOO|BAR|1234|A|B|C|D|E|F|G|H|
FOO|BAR|5678|I|J|K|L|M|N|O|P|Q|R|S|T|
$ cat chain.awk 
BEGIN   {FS = "|"}
$3==old {for(i = 4; i <= NF; i++) saved = saved (i>4?"|":"") $i}
$3!=old {if(old) print saved ; saved = $0 ; old = $3}
END     {print saved}
$ 
  • BEGIN we set the field separator
  • $3==old we append the fields $4 ... $NF to the saved data, joining the fields with | except for the first one (note that there is a last, null field)
  • $3!=old we print the saved data (except for the first record, when old is false) and we restart the mechanism
  • END we still have saved data in our belly, we have to print it

Upvotes: 1

jijinp
jijinp

Reputation: 2662

Another way:

 awk -F"|" '$3 in a{
     a[$3]=a[$3]"|"$4"|"$5"|"$6"|"$7;
     next
}
{ a[$3]=$0
}
END {
     for ( i in a) {
         print a[i]
     }
}'

Upvotes: 1

Related Questions