awksed

Printing everything except the first field with awk


I have a file that looks like this:

AE  United Arab Emirates
AG  Antigua & Barbuda
AN  Netherlands Antilles
AS  American Samoa
BA  Bosnia and Herzegovina
BF  Burkina Faso
BN  Brunei Darussalam

And I 'd like to invert the order, printing first everything except $1 and then $1:

United Arab Emirates AE

How can I do the "everything except field 1" trick?


Solution

  • Assigning $1 works but it will leave a leading space: awk '{first = $1; $1 = ""; print $0, first; }'

    You can also find the number of columns in NF and use that in a loop.


    From Thyag: To eliminate the leading space, add sed to the end of the command:

    awk {'first = $1; $1=""; print $0'}|sed 's/^ //g'