Search code examples
bashshellunixawkcut

Printing columns in bash


I am having a little problem with my script. I have a text file with several columns and what I want to do is print the last one (12th one) as a first one and then print columns no. 7, 8 and 9. I am able to print out columns 7, 8 and 9 with command:

awk -v f=7 -v t=9 '{for(i=f;i<=t;i++) printf("%s%s",$i,(i==t)?"\n":OFS)}' $file

where the $1 is the parameter for the file path

Any ideas how can I solve my little problem?


Solution

  • If you got a file like:

    bash-3.2$ cat myfile
    1 2 3 4 5 6 7 8 9 10 11 12
    a b c d e f g h i j  k  l
    

    Then simply

    bash-3.2$ awk '{print $NF, $7, $8, $9}' myfile
    12 7 8 9
    l g h i