匹配模式并使用awk或grep从文件打印相应的列 [英] match pattern and print corresponding columns from a file using awk or grep

查看:132
本文介绍了匹配模式并使用awk或grep从文件打印相应的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有重复标题的输入文件(下面):

  A1BG A1BG A1CF A1CF A2ML1 
aa bb cc dd ee
1 2 3 4 5

我想打印所有具有相同标题的列在一个文件中。例如上面的文件应该有三个输出文件; 1个用于2列的A1BG;第二次为A1CF 2列;对于A2ML1,第一列有1列。我有没有办法通过awk或grep使用单行程序来完成它?



我尝试了下面的一行:

  awk -vf =A1BG'!o {for(x = 1; x <= NF; x ++)if($ x == f){o = 1; next }} o {print $ x}'trial.txt 

但是这只在一列中搜索模式(在这种情况下为1)。我想查看所有标题名称并打印所有在其标题中包含A1BG的相应列。

me:



代码为step1.awk:



NR == 1 {PROCINFO [sorted_in] = @ind_num_asc
for(i = 1; i <= NF; i ++){f2c [$ i] =(f2c [$ i] ==)? $i:(f2c [$ i]\\t\$i)}}
NR == 2 {for(f2 in f2c)printf(%s:%s \ n,fn,f2c [fn])
exit
}

然后运行一个使用上面awk脚本的班轮:



awk -f step1.awk file.txt | awk -F:'BEGIN {print{}; {printprint$ 2,> \$ 1.txt\}; END {print}}'| awk -f --file.txt

这会输出制表符分隔的.txt文件,其中包含具有相同标题的所有列在一个文件中。 (为每种类型的头文件分开文件)

感谢Lars Fischer和其他人。



干杯


I have a input file with repetitive headers (below):

A1BG A1BG A1CF A1CF A2ML1
aa bb cc dd ee
1 2 3 4 5

I want to print all columns with same header in one file. e.g for above file there should be three output files; 1 for A1BG with 2 columns; 2nd for A1CF with 2 columns; 3rd for A2ML1 with 1 column. I there any way to do it using one-liners by awk or grep?

I tried following one-liner:

awk -v f="A1BG" '!o{for(x=1;x<=NF;x++)if($x==f){o=1;next}}o{print $x}' trial.txt

but this searches the pattern in only one column (1 in this case). I want to look through all the header names and print all the corresponding columns which have A1BG in their header.

解决方案

Following worked for me:

code for step1.awk:

NR == 1 { PROCINFO["sorted_in"] = "@ind_num_asc" for( i=1; i<=NF; i++ ) { f2c[$i] = (f2c[$i]=="")? "$" i : (f2c[$i] " \"\t\" $" i) } } NR== 2 { for( fn in f2c) printf("%s:%s\n", fn,f2c[fn]) exit }

Then run one liner which uses above awk script:

awk -f step1.awk file.txt | awk -F : 'BEGIN {print "{"}; {print " print " $2, "> \"" $1".txt" "\"" }; END { print "}" }'| awk -f - file.txt

This outputs tab delimited .txt files having all the columns with same header in one file. (separate files for each type of header)

Thanks Lars Fischer and others.

Cheers

这篇关于匹配模式并使用awk或grep从文件打印相应的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆