swapping two columns with awk keeping tabulation

Assuming you have a data file with N columns and you want to swap the first and second one, just type:

awk -F $'\t' ' { t = $1; $1 = $2; $2 = t; print; } ' OFS=$'\t' input_file

To keep the tabulation intact, you need to specify the separator.

Incoming separator is defined by -F $’\t’ and the separator for the output is defined by OFS=$’\t’.

Please follow and like us:
This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *