shellcsvcut

Rearrange columns using cut


I am having a file in the following format

Column1    Column2
str1       1
str2       2
str3       3

I want the columns to be rearranged. I tried below command

cut -f2,1 file.txt

The command doesn't reorder the columns. Any idea why its not working?


Solution

  • For the 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.

    It reaches field 1 first, so that is printed, followed by field 2.

    Use awk instead:

    awk '{print $2, $1}' file.txt