Unix 搜索和替换与模式不匹配的字符串



eg:

abcd |123 |234 |>?987 abc|erf
|urnch |<?utyf |poi 5423 one two lsk|

假设我有一个充满此类字符串的文件。我想用其他一些 caracter 重新系带管道 (|(,除了带有小于符号和问号的管道|<?.

结果:如果我想用美元($(替换管道,那么结果应该是

abcd $123 $234 |>?987 abc$erf
$urnch |<?utyf $poi 5423 one two lsk$

除了字符串有 | 所有其他堆都替换为美元。

通过 perl,

$ perl -pe 's/(?:|[<>]?)(*SKIP)(*F)||/$/g' file
abcd $123 $234 |>?987 abc$erf
$urnch |<?utyf $poi 5423 one two lsk$

另一个哎呀,

$ perl -pe 's/(?!|[<>]?)|/$/g' file
abcd $123 $234 |>?987 abc$erf
$urnch |<?utyf $poi 5423 one two lsk$

你也可以使用这个正则表达式,而不会产生负面的展望:

|([^<>])

使用此替换文本:

$$1

工作演示

最新更新