Mojo Dodo
Mojo Dodo

Reputation: 67

Switching columns of a csv-file with awk

I would like to switch column 2 with 3 in a csv-file

cat test.csv
1,1;1,2;1,3
2,1;2,2;2,3
3,1;3,2;3,3

I tried it with:

awk '{FS=";"; OFS=";"} { col1=$1;col2=$2;col3=$3; print col1,col3,col2 }' test.csv

but it results in:

;;1;1,2;1,3
;2,22,3
;3,23,3

What am I doing wrong?

Upvotes: 3

Views: 677

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133640

Following may help you on same.

awk -F";" '{print $1,$3,$2}' OFS=";"   Input_file

OR

awk -F";" '{gsub(.\r/,"");print $1,$3,$2}' OFS=";"   Input_file

In case your Input_file is having control M/carriage characters then use above code for same.

Upvotes: 3

Related Questions