I want to replace the 1st column of the file with the last one. So for this file:
A B C D E F
the result would be:
F B C D E A
I've tried to do so:
awk '{$a = $1; $1 = $NF; $NF = $a; print }' myfile > secfile
but didn't work.
I want to replace the 1st column of the file with the last one. So for this file:
A B C D E F
the result would be:
F B C D E A
I've tried to do so:
awk '{$a = $1; $1 = $NF; $NF = $a; print }' myfile > secfile
but didn't work.
As Wintermute says in comments, you have to use a
instead of $a
:
awk '{a = $1; $1 = $NF; $NF = a; print }' myfile > secfile
^
no $a !
This is because variables in awk
do not have leading $
. The $x
syntax is used to call the fields in the record, so that $x
stands for the field on the position stored in the variable x
.
You may use sed.
sed 's/^\([^\s]*\)\(.*\s\)\([^\s]*\)$/\3\2\1/' file