Tag Archives: C5 A3 B3 C3

OFS does not apply to few records in awk

By chidori

Hi ,

I am having a problem with my awk oneliner , which for some reason leaves the first two records

Input File

Code:

$ cat file1
A1:B1:C1:NoLimit
M1:M2:M3:Limit
A2:B2:C2,C3,C4,C5
A3:B3:C3,C4,C5,C6,C7


Desired output

Code:

A1,B1,C1,NoLimit
M1,M2,M3,Limit
A2,B2,C2
,,,C3
,,,C4
,,,C5
A3,B3,C3
,,,C4
,,,C5
,,,C6
,,,C7


I am using the below oneliner to achieve it partially but i dont get why the first two records OFS does not change to comma
,

Code:

nawk 'BEGIN{FS=":";OFS=","}{gsub(",","n,,,",$NF);print}' file1


This gives me an output

Code:

A1:B1:C1:NoLimit
M1:M2:M3:Limit

A2,B2,C2
,,,C3
,,,C4
,,,C5
A3,B3,C3
,,,C4
,,,C5
,,,C6
,,,C7


Can someone please help me understand the problem and correct the code

Source: FULL ARTICLE at The UNIX and Linux Forums