I have this sample file -
$cat f
a a1
a a2
a a3
b b1
b b2
b b3
b b4
c c1
c c2
c c3
And to print the record side by side for a common column I used below -
$awk '{a[$1]=a[$1]OFS$2} END {for (k in a) print k ,a[k]}' f
a a1 a2 a3
b b1 b2 b3 b4
c c1 c2 c3
How can I achieve below output in a single command, where last column is the count of record for a common column.
a a1 a2 a3 3
b b1 b2 b3 b4 4
c c1 c2 c3 3
we can combine two command to achive this -
$awk '{a[$1]=a[$1]OFS$2} END {for (k in a) print k ,a[k]}' f|awk '{print $0, NF-1}'
a a1 a2 a3 3
b b1 b2 b3 b4 4
c c1 c2 c3 3
One way is to use the return value of gsub - it returns number of substitutions made
$ awk '{a[$1]=a[$1]OFS$2}
END {for (k in a) print k a[k], gsub(OFS, OFS, a[k])}' ip.txt
a a1 a2 a3 3
b b1 b2 b3 b4 4
c c1 c2 c3 3
Note: I've also removed , between k a[k] - otherwise there'll be two spaces between 1st and 2nd columns
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With