Murlidhar Fichadia
Murlidhar Fichadia

Reputation: 2609

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

Upvotes: 0

Views: 179

Answers (2)

sjsam
sjsam

Reputation: 21965

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

Upvotes: 2

Murlidhar Fichadia
Murlidhar Fichadia

Reputation: 2609

Solution

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

Upvotes: 2

Related Questions