user3004220
user3004220

Reputation: 55

AWK script doesn't print last column of CSV file

I have the following awk script:

BEGIN { FS=","; OFS=","; } 
{ print $4,$1,$2; }

and this is my input file:

a1,a2,a3,a4
b1,b2,b3,b4

I'd expect it to return:

a4,a1,a2
b4,b1,b2

But it doesn't. Instead I get:

,a1,a2
,b1,b2

Why is that?

Upvotes: 2

Views: 1251

Answers (1)

anubhava
anubhava

Reputation: 784998

Most likely it is due to DOS line ending \r at the end of each line. Use this gnu-awk command to get the right output:

awk -v RS='\r?\n' 'BEGIN { FS=OFS="," } { print $4,$1,$2 }' file
a4,a1,a2
b4,b1,b2

Upvotes: 6

Related Questions