简体   繁体   中英

extract specific columns from dataset using AWK

I am trying to apply simple awk script to the dataset file. The file has 150 columns, I need cols between 20 to 30 only.

below is the script I used to get the records with field between 20 to 30.

code

BEGIN{}
{
for(f=20;f<=30;f++){
    print $f;
    }
}

I dont know why I get each value of the 10 fields in next line.

That is,

sample dataset

1 2 3 4 5 6 7
2 2 3 4 5 6 7 
3 3 3 4 5 6 7
4 4 4 4 5 6 7
5 5 5 5 5 6 7
6 6 6 6 6 6 7
7 7 7 7 7 7 7

I get output as 
1
2
3
4
5
6
7
2
2
3
4
5
6
7
...so on

Solution

BEGIN{FS=" ";}
{
for(f=20;f<=30;f++){
    printf("%s ",$f);
    }print "";
}

Below is another way of doing the same

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

Notes

  1. f and t are the starting and the ending columns respectively.
  2. We used the ternary operator to control the field separator between the needed columns.

Edit

If you need columns 20 thru 30 and the last column , below would suffice :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM