Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading