Sed + Regex 将匹配除以反引号开头的任何行

Sed + Regex which will match any line except those that start with a backtick

我正在尝试提出一个 sed 语句,该语句将匹配正则表达式以包含不以反引号开头的每一行。

目标是在不以反引号开头的每一行前加上一些内容。

反引号可以在行的开头或前面有一个或多个空白 space(或制表符),我需要匹配的任何其他行,即使是有反引号的行 开头不是.

这是我目前的情况:

cat myfile.txt
`this line starts with backtick`

  `this one does too, but there are some spaces`

!some stuff on May 14 19:52:58 2020

more stuff *(&) 243123123123
  indented stuff
  indented stuff

string here that is nothing special
*here is an asterisk

  match `this line`
  1 number one
  2 number two

 `ignore this one`

这是我的 sed 表达式(Mac)。

sed -e 's/^\([^[\s*`].*\)/--matched--/g' myfile.txt

`this line starts with backtick`

--matched--  `this one does too, but there are some spaces`

--matched--!some stuff on May 14 19:52:58 2020

--matched--more stuff *(&) 243123123123
--matched--  indented stuff
--matched--  indented stuff

string here that is nothing special
*here is an asterisk

--matched--  match `this line`
--matched--  1 number one
--matched--  2 number two

--matched-- `ignore this one`

编辑:清晰度

最简单的方法是匹配你想忽略的那些,然后否​​定匹配。这是一个 POSIX 兼容的方法:

sed --posix '/^[[:space:]]*`/!{/^$/!s/^/--matched---/}' ./myfile.txt

概念验证

$ sed --posix '/^[[:space:]]*`/!{/^$/!s/^/--matched---/}' ./myfile.txt
`this line starts with backtick`

  `this one does too, but there are some spaces`

--matched---!some stuff on May 14 19:52:58 2020

--matched---more stuff *(&) 243123123123
--matched---  indented stuff
--matched---  indented stuff

--matched---string here that is nothing special
--matched---*here is an asterisk

--matched---  match `this line`
--matched---  1 number one
--matched---  2 number two

  `ignore this one`

这可能适合您 (GNU sed):

sed  '/^\s*$\|^\s*`/!s/^/--matched--/' file

如果该行为空(也可能仅包含 spaces)或第一个非 space 字符是勾号,不要在前面添加 --matched--.