Column change order in unix csv file

Viewed 2604

I would like to change the column order in the below csv, on Solaris unix. As is:

cat mytest.csv
NAME,22015731,Fri Sep 01 05:30:21 2017,Fri Sep 01 05:45:26 2017

Desired:

cat mytest.csv
NAME,Fri Sep 01 05:30:21 2017,Fri Sep 01 05:45:26 2017,22015731

How is this possible to be done?

and if possible how can i do it for multiple files in a current dir?

thanks!

2 Answers

awk -F, '{ print $1","$3","$4","$2 }' mytest.cvs

should work. This is basic unix. -F option allow you to select the separator character of your fields in your line.

awk '{print $1,$3,$4,$2}' FS=, OFS=, mytest.csv > tmp; mv tmp mytest.csv

To work on multiple files, write a loop:

for file in $(generate-list-of-files): do
  ...
done
Related