Linux

多個文件上的 grep 問題並且沒有獲得所需的輸出

  • April 16, 2019

我在文件上使用以下命令根據 chr# (不同的染色體編號)提取幾行。這只是一個正在處理的文件。我有 8 個這樣的文件,對於每個文件,我必須對 chr(1to 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 vivek.das@ieo.eu
#$ -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

你需要這樣的東西:

{ 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

問題是重定向只影響一個命令。為了獲得重定向的輸出headgrep重定向,它們必須被分組。但awk這裡可能是更好的選擇。

引用自:https://unix.stackexchange.com/questions/181318