Reputation: 65
I have a big file like this one
C1,C2,C3
C1,C2
C5
C3,C5
I expected one output like this
C1,C2,C3 3
C1,C2 2
C5 1
C3,C5 2
I would like to make this using shell. Could you help me guys, please? Thankyou
Upvotes: 0
Views: 826
Reputation: 37404
Another in awk:
$ awk '{
m=(m<(n=length($0))?n:m) # get the max record length
a[NR]=$0 } # hash to a
END {
for(i=1;i<=NR;i++) # iterate and (below) output nicely
printf "%s%"(m-length(a[i])+4)"s\n",a[i],gsub(/,/,"&",a[i])+1 }
' file
C1,C2,C3 3
C1,C2 2
C5 1
C3,C5 2
IF you want to change the distance between fields and the length, toy with that +4
in the printf
.
Upvotes: 1
Reputation: 21965
Something like
awk 'BEGIN{FS=","}{printf "%-20s\t%d\n",$0,NF;}' file
should give
C1,C2,C3 3
C1,C2 2
C5 1
C3,C5 2
Note You need to adjust the width logically considering the maximum length of your lines
Upvotes: 2