Murlidhar Fichadia Murlidhar Fichadia - 5 months ago 25
Bash Question

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

Answer

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
Comments