grep 在多个文件上出现问题并且无法获得所需的输出

grep 在多个文件上出现问题并且无法获得所需的输出

我在文件上使用以下命令来根据 chr#(不同的染色体编号)提取几行。这只是正在处理的一个文件。我有 8 个这样的文件,对于每个文件,我必须对 chr 执行此操作(1 到 22,然后是 chrX 和 chrY),我没有使用任何循环,我单独执行此操作,但如果您看到我希望标头完好无损我的每一个输出。如果我单独执行,我会在输出中得到标头,但如果正在运行,但如果我一起运行所有 8 个文件的脚本(就像脚本中一个接一个的 8*24 命令),则输出没有任何标头。你能告诉我为什么会发生这种情况吗?

#!/bin/sh
#
#$ -N DOC_gatk_chr
#$ -cwd
#$ -e err_DOC_gatk_chr.txt
#$ -o out_DOC_gatk_chr.txt
#$ -S /bin/sh
#$ -M [email protected]
#$ -m bea
#$ -l h_vmem=25G

more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr1" > S_313_IPS_S7995.chr1.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr2" > S_313_IPS_S7995.chr2.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr3" > S_313_IPS_S7995.chr3.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr4" > S_313_IPS_S7995.chr4.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr5" > S_313_IPS_S7995.chr5.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr6" > S_313_IPS_S7995.chr6.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr7" > S_313_IPS_S7995.chr7.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr8" > S_313_IPS_S7995.chr8.coverage
more S_313_IPS_S7995.coverage.sample_interval_summary | head -n1; more S_313_IPS_S7995.coverage.sample_interval_summary | grep "chr9" > S_313_IPS_S7995.chr9.coverage

我正在使用 qsub 将其作为作业运行,因此脚本的结构如下所示。如果我单独执行命令,它会起作用,但如果我像这样运行它们,标题不会打印在输出文件中,即“;”似乎不被认可。我尝试使用 qsub filename.sh 和 sh filename.sh 来运行它。我发现使用 sh filename.sh 标题会打印在控制台中。所以肯定是';'之前的命令文件中未写入分号。我怎样才能摆脱这个问题。

期望的输出:

Target  total_coverage  average_coverage    IPS_S7995_total_cvg IPS_S7995_mean_cvg  IPS_S7995_granular_Q1   IPS_S7995_granular_median   IPS_S7995_granular_Q3   IPS_S7995_%_above_15
chr2:41460-41683    14271   63.71   14271   63.71   56  67  79  100.0
chr2:45338-46352    123888  122.06  123888  122.06  79  123 147 94.6
chr2:218731-218983  11653   46.06   11653   46.06   36  50  55  100.0
chr2:224825-225012  12319   65.53   12319   65.53   57  68  76  100.0
chr2:229912-230090  20983   117.22  20983   117.22  93  120 147 100.0
chr2:230947-231137  22386   117.20  22386   117.20  100 120 139 100.0
chr2:233074-233258  11710   63.30   11710   63.30   54  66  73  100.0
chr2:234086-234300  22952   106.75  22952   106.75  91  113 126 100.0
chr2:242747-242922  20496   116.45  20496   116.45  93  124 142 100.0
chr2:243469-243671  27074   133.37  27074   133.37  126 138 148 100.0

但我得到的输出低于没有标题的情况

chr2:41460-41683    14271   63.71   14271   63.71   56  67  79  100.0
chr2:45338-46352    123888  122.06  123888  122.06  79  123 147 94.6
chr2:218731-218983  11653   46.06   11653   46.06   36  50  55  100.0
chr2:224825-225012  12319   65.53   12319   65.53   57  68  76  100.0
chr2:229912-230090  20983   117.22  20983   117.22  93  120 147 100.0
chr2:230947-231137  22386   117.20  22386   117.20  100 120 139 100.0
chr2:233074-233258  11710   63.30   11710   63.30   54  66  73  100.0
chr2:234086-234300  22952   106.75  22952   106.75  91  113 126 100.0
chr2:242747-242922  20496   116.45  20496   116.45  93  124 142 100.0
chr2:243469-243671  27074   133.37  27074   133.37  126 138 148 100.0

答案1

你需要这样的东西:

{ head -n1 S_313_IPS_S7995.coverage.sample_interval_summary; 
  grep "chr1" S_313_IPS_S7995.coverage.sample_interval_summary; } >S_313_IPS_S7995.chr1.coverage

或者

awk 'NR==1 || /chr1/' S_313_IPS_S7995.coverage.sample_interval_summary >S_313_IPS_S7995.chr1.coverage

问题是重定向仅影响一个命令。为了获得重定向中head和的输出,必须对它们进行分组。grepawk这里可能是更好的选择。

相关内容