linux - Merging Multiple records into a Unique records with all the non-null values -
suppose have 3 records :
p1||1234| p1|56001|| p1|||nj i want merge these 3 records 1 attributes. final record :
p1|56001|1234|nj is there way achieve in unix/linux?
in example, 1st row have 1234, 2nd row have 56001.
i don't why in final result, 56001 goes before 1234. assume typo/mistake.
an awk-oneliner job:
awk -f'|' '{for(i=2;i<=nf;i++)if($i)a[$1]=(a[$1]?a[$1]"|":"")$i}end{print $1"|"a[$1]}' with data:
kent$ echo "p1||1234| p1|56001|| p1||nj"|awk -f'|' '{for(i=2;i<=nf;i++)if($i)a[$1]=(a[$1]?a[$1]"|":"")$i}end{print $1"|"a[$1]}' p1|1234|56001|nj
Comments
Post a Comment