awk - 在匹配 STRING 的前 8 行前添加 "#"

标签 awk sed text-parsing

这个问题有点令人困惑,所以我只举个例子。

假设我有以下情况:

$ grep -P "locus_tag\tM715_1000193188" Genome.tbl -B1 -A8
193188  193066  gene
            locus_tag   M715_1000193188
193188  193066  mRNA
            product hypothetical protein
            protein_id  gnl|CorradiLab|M715_1000193188
            transcript_id   gnl|CorradiLab|M715_mrna1000193188
193188  193066  CDS
        product hypothetical protein
        protein_id  gnl|CorradiLab|M715_1000193188
        transcript_id   gnl|CorradiLab|M715_mrna1000193188

我想在“locus_tag M715_1000193188”后面的8行加上“#”,这样我修改后的文件就变成这样:

193188  193066  gene
            locus_tag   M715_1000193188
#193188 193066  mRNA
#           product hypothetical protein
#           protein_id  gnl|CorradiLab|M715_1000193188
#           transcript_id   gnl|CorradiLab|M715_mrna1000193188
#193188 193066  CDS
#       product hypothetical protein
#       protein_id  gnl|CorradiLab|M715_1000193188
#       transcript_id   gnl|CorradiLab|M715_mrna1000193188

基本上,我有一个包含约 3000 个不同基因座标签的文件,对于其中的 300 个,我需要注释掉 mRNA 和 CDS 特征,因此 locus_tag 行之后的 8 行。

有什么方法可以用 sed 做到这一点?文件中还有其他类型的信息需要保持不变。

谢谢, 阿德里安

最佳答案

如果你可以使用 awk,应该这样做:

awk 'f&&f-- {$0="#"$0} /locus_tag/ {f=8} 1' file
193188  193066  gene
            locus_tag   M715_1000193188
#193188  193066  mRNA
#            product hypothetical protein
#            protein_id  gnl|CorradiLab|M715_1000193188
#            transcript_id   gnl|CorradiLab|M715_mrna1000193188
#193188  193066  CDS
#        product hypothetical protein
#        protein_id  gnl|CorradiLab|M715_1000193188
#        transcript_id   gnl|CorradiLab|M715_mrna1000193188

关于awk - 在匹配 STRING 的前 8 行前添加 "#",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29926593/

相关文章:

xml - 使用 Shell 更新 XML 属性的值

linux - curl, sed//有人知道从网络和进程中提取信息的更好方法吗?

mysql - 如何从mysql中的url中删除子域?

regex - 为什么 `\d` 在 sed 的正则表达式中不起作用?

linux - 有没有unix程序可以转换各种HH :MM:SS formats to seconds?

linux - 删除非数值 - Unix

Powershell:将下一行与当前行合并

c# - 正则表达式模式与某些节目标题不匹配

sorting - 如何比较两个文件并打印两个文件不同的值

用于合并由空行分隔的行的 Shell 脚本