I would like to extract tab-delimited columns from a text file ("columns.txt") in which the header (first line) matches certain strings listed in another text file ("strings.txt").
"columns.txt" looks like this:
rs2438689 rs54666437 rs9877702046 rs025436779...
0 0 0 1
1 1 2 2
0 1 2 0
... ... ... ...
"strings.txt" looks like this:
rs2438689
rs9877702046
...
The output text file "output.txt" should look like this (tab-delimited):
rs2438689 rs9877702046...
0 0
1 2
0 2
... ...
Any suggestions on how to do this with awk? Thank you!
awk -F '\t' -v OFS='\t' 'FNR==1{for(i=1;i<=NF;++i)if($i!~/rs2438689/)k[i]=1}{n=split($0,f,FS);$0=j="";for(i=1;i<=n;++i)if(i in k)$(++j)=f[i]}1'
Problem 1: How to read in multiple strings from another text file? How to keep columns matching the string and delete the rest? Thank you! – mitch May 27 '19 at 15:01