Shell – Swap first and second columns in a CSV file

awkcsvshell-scripttext processing

I'm using bash. I have a CSV file with two columns of data that looks roughly like this

 num_logins,day
 253,2016-07-01
 127,2016-07-02

I want to swap the first and second columns (making the date column the first one). So I tried this

awk ' { t = $1; $1 = $2; $2 = t; print; } ' /tmp/2016_logins.csv 

However, the results are outputting the same . What am I missing in my awk statement above to get things to switch properly?

Best Answer

Here you go:

awk -F, '{ print $2 "," $1 }' sampleData.csv 
Related Question