using awk for reformatting text

128 Views Asked by At

I have a large file in this format (I have in both csv and text format and the first column is IDs):

9   KIDL1   1
9   KIDL1   1
9   KIDL2   0
9   KIDL2   1
9   KIDL3   1
9   KIDL3   1

and want to change it to :

    KIDL1   KIDL2   KIDL3
9   1/1     0/1     1/1

is it possible using awk commands or related programs?

1

There are 1 best solutions below

0
Digvijay S On

Not the perfect solution. but I managed to generate the desired output based on input provided.

awk '{a[$1" "$2]=$3"/"a[$1" "$2]} END {for (i in a)  {b=b" "i; c=c" "a[i];} {gsub(/ +[0-9]/, " ",b); gsub(/\/ +/, " ",c ) }  print b"\n"$1,c }'

Demo:

$awk '{a[$1" "$2]=$3"/"a[$1" "$2]} END {for (i in a)  {b=b" "i; c=c" "a[i];} {gsub(/ +[0-9]/, " ",b); gsub(/\/ +/, " ",c ) }  print b"\n"$1,c }' < file1.txt

  KIDL1  KIDL2  KIDL3
9  1/1 1/0 1/1/
$