I have cut the fields of a csv file as cut -d "," -f 32,4,6,43,12,9. sample CSV file has columns fruit1 veg2 flowers3 animals4 birds5

I am cutting this file to have columns flowrs3 veg2 birds5 here output of file does not come in the order specified in cut.

Instead it comes in ascending order of column number. ie. veg2 flowers3 birds5

I want the output of file to be displayed in the order of column number specified in the cut.

  • Can you give us a sample output of the file & the desired output required. – Mongrel Dec 15 '16 at 7:10

According to the man page, cut can't reorder columns. The selected fields are written at the same order that they are read.

cut(1) man page:

Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once.

But you can do what you need using awk:

awk -F "," '{print($32,$4,$6,$43,$12,$9)}' text.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.