grep 命令报错

Ron*_*Ron 5 grep

我正在使用 grep 过滤掉某些模式(在我的例子中是基因)的内容。欲了解更多信息,这里是较早的链接。

从另一个文件中列出的文件中查找模式

我的代码(应该工作)但不是。

 grep -f file1 file2
Run Code Online (Sandbox Code Playgroud)

这是我的基因子集(file1):

C1QTNF3
C5orf22
C5orf28
C5orf34
C5orf38
C5orf42
C5orf49
C5orf51
C5orf64
C6
C7
C9
CAPSL
CARD6
CARTPT
CCDC125
CCDC152
CCL28
CCNB1
CCNO
CCT5
CD180
CDC20B
CDH10
CDH12
CDH18
CDH6
CDH9
CDK7
CENPH
CENPK
CKMT2
CLPTM1L
CMBL
CMYA5
COL4A3BP
CR749689
CRHBP
CRSP8P
CT49
CTNND2
CWC27
DAB2
DAP
DDX4
DEPDC1B
DHFR
DHX29
DIMT1
DMGDH
Run Code Online (Sandbox Code Playgroud)

下面是我的文本文件(file2),它正在匹配,即使文件 1 中没有基因 UNC79,如 SNPEFF_GENE_NAME=UNC79 所示,文件 2 中显示存在。

  AC=3;AF=0.016;AN=186;BaseQRankSum=0.075;DB;DP=292;Dels=0.00;FS=4.271;HaplotypeScore=0.0891;InbreedingCoeff=0.0225;MLEAC=2;MLEAF=0.011;MQ=59.18;MQ0=1;MQRankSum=0.969;QD=13.42;ReadPosRankSum=-0.373;SNPEFF_EFFECT=INTRON;SNPEFF_EXON_ID=23;SNPEFF_FUNCTIONAL_CLASS=NONE;SNPEFF_GENE_BIOTYPE=protein_coding;SNPEFF_GENE_NAME=UNC79;SNPEFF_IMPACT=MODIFIER;SNPEFF_TRANSCRIPT_ID=ENST00000256339;VQSLOD=9.31;culprit=DP
Run Code Online (Sandbox Code Playgroud)

因此,grep 的输出是来自 file2 的整个文本 blob。

下面是文件中的完整行,它给出了问题。第二列是基因名称。我的file1中没有这个基因。所以我不想要这个特定行的输出。我有 1000 行这样的不同基因,只需要过滤掉文件 1 中的基因。

    intronic    UNC79   14  94062922    94062922    A   G   het 80.54   3   14  94062922    rs183710732 A   G   80.54   PASS    AC=3;AF=0.016;AN=186;BaseQRankSum=0.075;DB;DP=292;Dels=0.00;FS=4.271;HaplotypeScore=0.0891;InbreedingCoeff=0.0225;MLEAC=2;MLEAF=0.011;MQ=59.18;MQ0=1;MQRankSum=0.969;QD=13.42;ReadPosRankSum=-0.373;SNPEFF_EFFECT=INTRON;SNPEFF_EXON_ID=23;SNPEFF_FUNCTIONAL_CLASS=NONE;SNPEFF_GENE_BIOTYPE=protein_coding;SNPEFF_GENE_NAME=UNC79;SNPEFF_IMPACT=MODIFIER;SNPEFF_TRANSCRIPT_ID=ENST00000256339;VQSLOD=9.31;culprit=DP    GT:AD:DP:GQ:PL  0/1:1,2:3:33:39,0,33
Run Code Online (Sandbox Code Playgroud)

slm*_*slm 7

file2包含一个grepfile1.

例子

                      党卫军#1

您可以在上面的屏幕截图中看到它,文本以红色突出显示grep。顺便说一句,您可能也想使用颜色突出显示功能:

$ grep --color=auto -f file1 file2
Run Code Online (Sandbox Code Playgroud)

但我只想匹配整个单词?

如果您只想grep返回“整个单词”的匹配项,您可以包含-w开关。这只会返回匹配是使用“单词”来自 的整个单词匹配file1

在这里,我创建了另一个file1a包含基因的文件 ( ) UNC79

$ grep C7 file1 file1a
file1:C7
file1a:C7
file1a:UNC79
Run Code Online (Sandbox Code Playgroud)

在这里,当我运行grep -wf ...与2个索引文件(命令file1file1a),你可以看到,我们得到敌不过file1和火柴file1a

           党卫军#2

摘自 grep 手册页

   -w, --word-regexp
          Select only those lines containing matches that form whole words.
          The test is that the matching substring must  either  be  at  the
          beginning  of  the  line,  or  preceded by a non-word constituent
          character.  Similarly, it must be either at the end of the line or
          followed by a non-word constituent character.  Word-constituent
          characters are letters, digits, and the underscore.
Run Code Online (Sandbox Code Playgroud)

这个技巧适用于@Ron 的情况,因为他的基因名称以非单词字符 ( =)为边界,并以 ( ;)结尾。否则这个技巧可能不会奏效。


ter*_*don 6

由于您的基因名称始终位于文件的第二列中,因此您可以使用awk

awk '
    {   ## while reading the first file, save name in the array a
        if(NR==FNR){a[$1]++;} 

        ## If this is the 2nd file
        else{
            ## print if the value of the second column is defined in the array 
            if($2 in a){print}
        }
    }' file1 file2
Run Code Online (Sandbox Code Playgroud)

同样,浓缩:

awk '{if(NR==FNR){a[$1]++;}else{if($2 in a){print}}}' file1 file2 
Run Code Online (Sandbox Code Playgroud)

更浓缩:

awk '(NR==FNR){a[$1]++}($2 in a){print}' file1 file2 
Run Code Online (Sandbox Code Playgroud)

和真正的极简主义(回答@Awk):

awk 'NR==FNR{a[$1]}$2 in a' file1 file2 
Run Code Online (Sandbox Code Playgroud)