Thursday, April 17, 2014

How to swap two or more columns in a text file in UNIX using awk

If you need to swap two columns in a text file use awk.
To swap colums 1 and 2, in Unix terminal type:
awk '{ t=$1 ; $1=$2; $2=t; print }' inputfile > outputfile

To swap columns 1, 2, 3 to the order 3, 1, 2, type:
awk '{ t=$1 ; $1=$3; $3=t; print }' inputfile > temp
awk '{ t=$2 ; $2=$3; $3=t; print }' temp > outputfile

No comments:

Post a Comment