awk adding a semicolon if empty

I do have a file with different length of each line. E.g.:

a; 1; 2; 3; 4;  
b; 11; 22;  
c; 122; 233; 344; 45; 56;  
d; 13;  
e; 144; 25; 36; 47; 58; 69;

I try to generate a file, separated by semicolon where each line has the same amount of values. E.g.:

a; 1; 2; 3; 4; ; ;  
b; 11; 22; ; ; ; ;  
c; 122; 233; 344; 45; 56; ;  
d; 13; ; ; ; ; ;  
e; 144; 25; 36; 47; 58; 69;

I tried different ways with awk but I am to newbie to get it done correctly in bulk.

awk '{if( $4 == ""){print ";"}else{print $4}}' testtest.txt

I hope the swarm intelligence can help me with it.

>Solution :

Making your records contain at least 8 fields:

awk -F '; *' -v OFS='; ' '{$8 = $8} 1'

Leave a Reply