Reputation: 307
I've a csv file like below.
id,id1,id2,id3,id4,id5
1,101,102,103,104
2,201,202,203
3,301,302
Now what i want to add comma(,) to each line to make all line with same number of delimiters. So desired output should be.
id,id1,id2,id3,id4,id5
1,101,102,103,104,
2,201,202,203,,
3,301,302,,,
Using
awk -F "," ' { print NF-1 } ' file.csv | sort -r | head -1
I am able to find the max occurance of delimiter but not sure how to compare each line and append comma if its less than max.
Upvotes: 2
Views: 145
Reputation: 23667
With GNU awk
(as I do not know if this works for other implementations)
$ # simply assign value to NF
$ awk -F, -v OFS=',' '{NF=6} 1' ip.txt
id,id1,id2,id3,id4,id5
1,101,102,103,104,
2,201,202,203,,
3,301,302,,,
If first line determines number of fields required:
$ awk -F, -v OFS=',' 'NR==1{f=NF} {NF=f} 1' ip.txt
id,id1,id2,id3,id4,id5
1,101,102,103,104,
2,201,202,203,,
3,301,302,,,
If any line determines max field:
$ cat ip.txt
id,id1,id2
1,101,102,103
2,201,202,203,204
3,301,302
$ awk -F, -v OFS=',' 'NR==FNR{f=(!f || NF>f) ? NF : f; next} {NF=f} 1' ip.txt ip.txt
id,id1,id2,,
1,101,102,103,
2,201,202,203,204
3,301,302,,
Upvotes: 4
Reputation: 92854
Unified awk
approach (based on number of fields of the 1st header line):
awk -F',' 'NR==1{ max_nf=NF; print }
NR>1{ printf "%s%.*s\n", $0, max_nf-NF, ",,,,,,,,," }' file
The output:
id,id1,id2,id3,id4,id5
1,101,102,103,104,
2,201,202,203,,
3,301,302,,,
Or via loop:
awk -F',' 'NR==1{ max_nf=NF; print }
NR>1{ n=max_nf-NF; r=""; while (n--) r=r","; print $0 r }' file
Upvotes: 2
Reputation: 133528
Following awk
may also help you on same.
awk -F, '
FNR==1{
val=NF;
print;
next
}
{
count=NF;
while(count<val){
value=value",";
count++};
print $0 value;
value=count=""
}
' Input_file
Output will be as follows:
id,id1,id2,id3,id4,id5
1,101,102,103,104,
2,201,202,203,,
3,301,302,,,
Upvotes: 2
Reputation: 2662
awk -F"," '{i=NF;c="";while (i++ < 6) {c=c","};print $0""c}' file
Output:
id,id1,id2,id3,id4,id5
1,101,102,103,104,
2,201,202,203,,
3,301,302,,,
Upvotes: 2
Reputation: 189417
You are already using the variable NF
which indicates how many fields there are on a line.
awk -F , 'NF<6 { OFS=FS; for (i=NF+1; i<=6; i++) $i="" }1' filename
We start looping at the first undefined field and set it to an empty string, until we have six fields. Then the 1
at the end takes care of printing the now fully populated line. The OFS=FS
is necessary to make the output field separator also be a comma (it is a space by default).
Upvotes: 2