Forcing the order of output fields from cut command
This can't be done using cut
. According to the man page:
Selected input is written in the same order that it is read, and is written exactly once.
Patching cut
has been proposed many times, but even complete patches have been rejected.
Instead, you can do it using awk
, like this:
awk '{print($2,"\t",$1)}' abcd.txt
Replace the \t
with whatever you're using as field separator.
Lars' answer was great but I found an even better one. The issue with his is it matches \t\t as no columns. To fix this use the following:
awk -v OFS=" " -F"\t" '{print $2, $1}' abcd.txt
Where:
-F"\t"
is what to cut on exactly (tabs).
-v OFS=" "
is what to seperate with (two spaces)
Example:
echo 'A\tB\t\tD' | awk -v OFS=" " -F"\t" '{print $2, $4, $1, $3}'
This outputs:
B D A