1

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Rmd
  • 11
  • 1

2 Answers2

2

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

You may use sed.

sed 's/^\([^\s]*\)\(.*\s\)\([^\s]*\)$/\3\2\1/' file
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274