Hi All,
I have a file. Each line suppose to contain 188 fields delimited by , (comma --> so 187 commas) but some records have less than 188 fields which needs to be filled by additional commas at the end.
Ex: a file wth 5 columns and 4 commas
test1,test2,test3
red1,red2
blue1,blue2,blue3,blue4
bad1
good1,good2,good3,good4,good5
Output should be:
test1,test2,test3,,
red1,red2,,,
blue1,blue2,blue3,blue4,
bad1,,,,
good1,good2,good3,good4,good5
Any help is appreciated.
Thanks,
Jingi
hi jingi,
here is the solution:
awk -F"," '
{
str=""
for (i=NF;i<=5;i++)
{
str=sprintf("%s%s",str,",")
}
printf("%s%s\n",$0,str)
}
' filename
Note: Change the 5 in for loop to number of "," you want in file at end.
regards,
rishi
Ygor
September 27, 2005, 1:03am
3
Try...
awk 'BEGIN{FS=OFS=","}{for(x=1;x<=n;x++)printf $x (x==n?ORS:OFS)}' n=188 file1 > file2
try this
DEFAULT=5
j=1
for i in `awk -F"," '{print NF}' <filename>`
do
final=`awk -F"," '{if( NR == '$j' ) print $0}' <filename>`
while [ $i -lt $DEFAULT ]
do
final=$final","
i=$(($i + 1))
done
echo "$final"
j=$(($j + 1))
done
Hi all,
I am getting the following error:
awk: record `SMAU,9628,SYDBUL083F...' has too many fields
Please help. Try the above scripts with 187 commas (required)
Thanks
Works fine for me. *shrug*