Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于bash中的输入查询文件_Bash_Awk_Grep - Fatal编程技术网

基于bash中的输入查询文件

基于bash中的输入查询文件,bash,awk,grep,Bash,Awk,Grep,我想查询一个巨大的压缩基因组文件(dbsnp.gz),其格式如下: chr1 196575462 rs115411599 A G . . RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0

我想查询一个巨大的压缩基因组文件(dbsnp.gz),其格式如下:

chr1    196575462   rs115411599 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1
我想通过查询前两列或第三列(或所有三列,如果这能加快速度的话)来提取第4列和第5列。第1+2列或第3列足以区分这些条目

我的输入文件(file.txt)有50多个实体:

chr1:196575462   rs115411599
我最初考虑使用awk和zgrep

for i in $(awk -F'\t' '{print $1}' file.txt); do

'{$i == $(zcat dbsnp.gz | awk -F'\t' {print $1,$2} OFS=":"})' '{print $0}'

done

这不行吗?我应该坚持awk吗?或者使用zgrep?

[更新了Ed Morton的提示-谢谢]

假设:

  • 列1+2的组合在
    file.txt中是唯一的
  • 第3列在
    file.txt中是唯一的
从一些示例数据文件开始:

$ cat gene.dat
chr1    196575462   rs115411599 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1
chr2    196575462   rs115411588 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1
chr3    196575462   rs115411577 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1

# go ahead and create a *gz file we can use with zcat:

$ gzip -c gene.dat > gene.dat.gz

$ cat file.txt
chr1:196575462   rs115411599
chr2:196500077   rs115400077
chr3:196500088   rs115400088
一个
awk
解决方案:

awk '
# for first file, use fields 1+2 and 3 as indexes for our associative arrays

FNR==NR { match1[$1,$2] ; match2[$3] ; next }

# for second file, if fields 1+2 matches an index in the match1[] array then print fields $4/$5; if we found a match then go to next line in file

( ($1,$2) in match1 ) { print $4,$5 ; next }

# for second file, if field 3 matches an index in the match2[] array then print fields $4/$5

(      $3 in match2 ) { print $4,$5 }

  # for our first file we will replace : with <space> ; this gives us 3 fields from first file
  # for our second file we will feed the results of the zcat operation
' <(sed 's/:/ /g' file.txt) <(zcat gene.dat.gz)

这种方法的一大好处是,我们只扫描每个文件一次(例如,问题中的示例代码显示,
zcat dbsnp.gz
循环中反复运行/扫描)。

[Ed Morton的更新提示-谢谢]

假设:

  • 列1+2的组合在
    file.txt中是唯一的
  • 第3列在
    file.txt中是唯一的
从一些示例数据文件开始:

$ cat gene.dat
chr1    196575462   rs115411599 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1
chr2    196575462   rs115411588 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1
chr3    196575462   rs115411577 A   G   .   .   RS=115411599;RSPOS=196575462;dbSNPBuildID=132;SSR=0;SAO=0;VP=0x050000080005040436000100;GENEINFO=KCNT2:343450;WGT=1;VC=SNV;INT;ASP;VLD;HD;KGPhase1;KGPhase3;CAF=0.9994,0.000599;COMMON=1

# go ahead and create a *gz file we can use with zcat:

$ gzip -c gene.dat > gene.dat.gz

$ cat file.txt
chr1:196575462   rs115411599
chr2:196500077   rs115400077
chr3:196500088   rs115400088
一个
awk
解决方案:

awk '
# for first file, use fields 1+2 and 3 as indexes for our associative arrays

FNR==NR { match1[$1,$2] ; match2[$3] ; next }

# for second file, if fields 1+2 matches an index in the match1[] array then print fields $4/$5; if we found a match then go to next line in file

( ($1,$2) in match1 ) { print $4,$5 ; next }

# for second file, if field 3 matches an index in the match2[] array then print fields $4/$5

(      $3 in match2 ) { print $4,$5 }

  # for our first file we will replace : with <space> ; this gives us 3 fields from first file
  # for our second file we will feed the results of the zcat operation
' <(sed 's/:/ /g' file.txt) <(zcat gene.dat.gz)


这种方法的一大好处是,我们只扫描每个文件一次(例如,问题中的示例代码显示,
zcat dbsnp.gz
循环中反复运行/扫描)。

您不能像尝试的那样混合使用shell和awk。确切的标准和所需的输出是什么?我想从dbsnp.gz中提取记录,其中$1':'$2与文件中的$1匹配。txt是否要
zgrep“^chr1”dbsnp.gz
?zgrep需要很长时间。想和awk做比较!在
while
循环中运行
zcat
(或
zgrep
)意味着重复扫描
巨大的压缩基因组文件
,您不能像尝试的那样混合使用shell和awk。确切的标准和所需的输出是什么?我想从dbsnp.gz中提取记录,其中$1':'$2与文件中的$1匹配。txt是否要
zgrep“^chr1”dbsnp.gz
?zgrep需要很长时间。想和awk做比较!在
循环中运行
zcat
(或
zgrep
),而
循环意味着重复扫描
巨大的压缩基因组文件
,我认为FNR就是出于这个原因。这比循环快多了!感谢您在比较中使用
match2[$3]
将使
match2[]
比您在match2
中使用
$3时要大得多,即两个输入文件中所有唯一值的大小,而不仅仅是
file.txt
中的列表。
match1[]
同上。当您更改为在
中使用
进行比较时,您也不再需要将数组项分配给FNR==NR块中的
=1
。@EdMorton没有完全理解您的评论;建立所以仅仅测试一个数组条目就会创建一个数组条目,对吗?我已经用你的建议更新了答案。。。我知道不需要paren,但是(对我来说)它们提供了更多的清晰性测试,即填充数组条目将创建一个数组条目。测试是否存在数组索引不起作用。因此
if(foo[7])
将创建
foo[7]
如果它不存在(像未设置的标量变量一样填充零或null),而
if(foo中的7)
将不创建
foo[7]
。如果你的字符串连接比<>代码>在< /代码>测试中添加了更高的优先级,那么,在Matk1中,$ Matlab 2中的“Matk1</CODE>$ 1”,“$ 2”在IDCK中为IDK,并且添加PARS使代码变得更清晰。最后-考虑使用<代码> MatCH1 [ $ 1,$2 ] < /C>和<代码>($1,2)在match1
中,不使用与
的串联,因为
就是这样做的,
子集
)存在。我假设FNR就是出于这个原因。这比循环快多了!感谢您在比较中使用
match2[$3]
将使
match2[]
比您在match2
中使用
$3时要大得多,即两个输入文件中所有唯一值的大小,而不仅仅是
file.txt
中的列表。
match1[]
同上。当您更改为在
中使用
进行比较时,您也不再需要将数组项分配给FNR==NR块中的
=1
。@EdMorton没有完全理解您的评论;建立所以仅仅测试一个数组条目就会创建一个数组条目,对吗?我已经用你的建议更新了答案。。。我知道不需要paren,但是(对我来说)它们提供了更多的清晰性测试,即填充数组条目将创建一个数组条目。测试是否存在数组索引不起作用。因此
if(foo[7])
将创建
foo[7]
如果它不存在(像未设置的标量变量一样填充零或null),而
if(foo中的7)
将不创建
foo[7]
。如果你的字符串连接比<>代码>在< /代码>测试中添加了更高的优先级,那么,在Matk1中,$ Matlab 2中的“Matk1</CODE>$ 1”,“$ 2”在IDCK中为IDK,并且添加PARS使代码变得更清晰。最后-考虑使用<代码> MatCH1 [ $ 1,$2 ] < /C>和<代码>($1,2)在match1
中,不使用与
的串联,因为
就是这样做的,
子集
)。