从文件中提取列 [英] Extracting columns from a file
问题描述
我有一个包含一张大桌子的文件,这样的事情:
I have a file containing a big table, something like that :
Header1 Header2 Header3 ... Header8031
Value1 Value2 Value3 .... Value8031
.
.
Value1 Value2 Value3 ... Value8031
在另一个文件中我与previous表的一些标题的列表。
In another file I have a list with some headers of the previous table.
Header1
Header3000
Header5
Header200
Header10
我想以提取在表中的信息仅在列表中的标头。换句话说,获得与列表上的报头相匹配的列。 [匹配拥有ID表上的名单]
I want to extract the information in the table only for the headers in the list. In other words, getting the columns that match with the headers on the list. [matching the list with the columns id on the tables]
输出
Header1 Header3000 Header5 Header200 Header10
Value1 Value3000 Value5 Value200 Value10
Value1 Value3000 Value5 Value200 Value10
我试着用 AWK
一些例子(<一个href=\"http://stackoverflow.com/questions/11098189/awk-extract-columns-from-file-based-on-header-selected-from-2nd-file\">AWK提取的基础上,从2号文件选择)头文件列,但我不能让我的期望的输出。
I tried some examples with awk
(AWK extract columns from file based on header selected from 2nd file), but I'm not able to get my desired output.
推荐答案
这AWK线,你将工作:
this awk line would work for you:
awk 'NR==FNR{a[$0]=7;next}FNR==1{for(i=1;i<=NF;i++)if(a[$i])c[++x]=i}
{for(i=1;i<=x;i++)printf "%s%s", $(c[i]), (i==x?RS:FS)}' headerFile dataFile
测试与例如:
kent$ head col f
==> col <==
Header1
Header3
Header5
==> f <==
Header1 Header2 Header3 Header4 Header5 Header10
Value1 Value2 Value3 Value4 VAlue5 Value10
Value1 Value2 Value3 Value4 Value5 Value10
kent$ awk 'NR==FNR{a[$0]=7;next}FNR==1{for(i=1;i<=NF;i++)if(a[$i])c[++x]=i}
{for(i=1;i<=x;i++)printf "%s%s", $(c[i]), (i==x?RS:FS)}' col f
Header1 Header3 Header5
Value1 Value3 VAlue5
Value1 Value3 Value5
这篇关于从文件中提取列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!