Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select some columns with AWK? [closed]

Tags:

linux

awk

I want to select some columns in a file and run some command on it. so my script is this

awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$19}' test.txt > outpot.txt

but this print it to another file and I tried to do this

awk '{print $1,$2,$3,$4,$5,$6,$7,$9,$10,$11,$12,$13,$14,$15,$16}' test.txt | next commands

(This commands works fine! I did a mistake and I don't know how to remove this question)

is it possible to make this command shorter like instead of writing all columns just write $1-7 && $9-15 && $19 (but this is not really important I just wondered if it's possible). The main thing is to be able to choose that columns

like image 482
mahmood Avatar asked Jan 23 '12 14:01

mahmood


2 Answers

Updated based on glennjackman's suggestion:

awk '{for (i=1;i<=NF;i++) if ((1<=i && i<=7) || (9<=i && i<=15) || i==19) printf("%s ", $i); print ""}' file
like image 131
jaypal singh Avatar answered Nov 14 '22 05:11

jaypal singh


To answer one part of your question, in awk script you can do:

{
   for (i=1; i<=7; i++)
      print $i;
   for (i=9; i<=15; i++)
      print $i;
   print $19;
}
like image 3
anubhava Avatar answered Nov 14 '22 07:11

anubhava