# Extract columns where header matches a given string

**URL:** <https://community.unix.com/t/extract-columns-where-header-matches-a-given-string/284791>\
**Category:** Shell Programming and Scripting\
**Created:** [March 17, 2011, 4:05pm UTC](https://community.unix.com/t/extract-columns-where-header-matches-a-given-string/284791 "2011-03-17T16:05:52Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![pravin27](https://community.unix.com/letter_avatar/pravin27/32/5_5575768a8748004e209b776fc1b2916d.png) [@pravin27](https://community.unix.com/u/pravin27)\
**Post date:** [March 19, 2011, 1:43am UTC](https://community.unix.com/t/extract-columns-where-header-matches-a-given-string/284791/22 "2011-03-19T01:43:52Z")

</div>

As per order by file1

```nohighlight
 awk 'NR==FNR{a[++k]=$1;next} {if(FNR==1){for(m=1;m<=k;m++){for(i=1;i<=NF;i++){if(a[m]==$i){printf a[m]" ";b=$i}}}}else{printf "\n";for(j=1;j<=NF;j++){for(s in b) {if(s==j) {printf $j" "}} }}}END {printf "\n"}' file1 file2

```

[Previous page](https://community.unix.com/t/extract-columns-where-header-matches-a-given-string/284791.md?page=1)
