Here is an example text file:
A B C D E F G
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
I would like to extract specific columns based on the values on the 3rd row, that is, 2 3 4 5 6 7 8. Let's say, I would like to extract all the columns with a value on the 3rd row larger than 5. That will be the final 3 columns. Hence, my goal is to select and generate the following:
E F G
5 6 7
6 7 8
7 8 9
Here is my code:
NR==3 {
for (i=1; i<=NF; i++) {
if ($i > 5) x[j++] = i
}
}
NR>= 1 {
for (i=0 ; i < j-1; i++ )
printf("%s ",$x[i])
printf("%s\n",$x[j-1])
}
However, this generates the following:
A B C D E F G
1 2 3 4 5 6 7
6 7 8
7 8 9
What did I miss?
NR>=1code block, with no initialization forjandx