如何用sed替换除特定模式之外的所有内容?

gus*_*a10 4 sed

我想替换除我的 ::ID 和 [ID2] 之外的所有内容,但无法真正找到一种方法来使用 sed 并保持匹配,有什么建议吗?

例如:

TRINITY_DN75270_c3_g2::TRINITY_DN75270_c3_g2_i4::g.22702::m.22702 [sample]
Run Code Online (Sandbox Code Playgroud)

我想拥有:

TRINITY_DN75270_c3_g2_i4[sample]
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

Dop*_*oti 5

对于提供的给定输入,此sed表达式似乎可以满足您的要求:

$ cat input
`>TRINITY_DN75270_c3_g2::TRINITY_DN75270_c3_g2_i4::g.22702::m.22702 [sample]`
$ sed 's/^.*::\([A-Z_0-9a-z]*\)::.*\[\(.*\)\].*/\1[\2]/' input
TRINITY_DN75270_c3_g2_i4[sample]
Run Code Online (Sandbox Code Playgroud)

神奇之处在于使用正则表达式组和两个反向引用来重建所需的输出。阐述:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
  ::                       '::'
  \(                       group and capture to \1:
    [A-Z_0-9a-z]*            any character of: 'A' to 'Z', '_', '0'
                             to '9', 'a' to 'z' (0 or more times
                             (matching the most amount possible))
  \)                       end of \1
  ::                       '::'
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
  \[                       '['
  (                        group and capture to \2:
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
  )                        end of \2
  \]                       ']'
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
Run Code Online (Sandbox Code Playgroud)

\1您想要提取的第一个键也是如此,然后\2是方括号中的任何内容。然后由 重建\1[\2]/,创建您想要的输出。

  • 我为“sed”命令中使用的表达式添加了详细的逐条指南。 (2认同)