With awk
for example:
$ awk '{a=$2; $2=$3; $3=a}1' file
a c b d e
e g f h i
p r q s t
You can make it more general with:
$ awk -v c1=FIRST_COL -v c2=SECOND_COL '{a=$c1; $c1=$c2; $c2=a}1' file
For example:
$ awk -v c1=2 -v c2=3 '{a=$c1; $c1=$c2; $c2=a}1' file
a b d c e
e f h g i
p q s r t
To use it from inside vi
, use the following before the command:
:%!
So that the full command to execute is:
:%!awk '{a=$2; $2=$3; $3=a}1'