How to select some columns with AWK?

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

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

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

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

(These commands work fine! I made a mistake and I don’t know how to remove this question)

Is it possible to make this command shorter so that instead of writing all the columns, just write $1-7 && $9-15 && $19 (but this is not very important, I just wondered if this is possible). The main thing is to be able to select columns

+6
source share
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 
+2
source

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; } 
+2
source

Source: https://habr.com/ru/post/906673/


All Articles