I have a file with 3 columns like this
JavaScript
x
Col1 Col2 Col3
A B <-
C D ->
E F ->
I want to swap the entries of the Col1 and Col2 whenever there is
JavaScript
<-
in the third column. I want my output file to be like
JavaScript
Col1 Col2 Col3
B A ->
C D ->
E F ->
Advertisement
Answer
JavaScript
awk '($3=="<-"){$3=$2;$2=$1;$1=$3;$3="->"}1' <file>
Essentially, if $3=="<-"
, then swap the columns and redefine $3
. Then print.