简体   繁体   English

搜索和替换反斜杠,除了一件事

[英]Search and replace backslash except one thing

I want to replace \ with \\ except \N .我想将\替换为\\除了\N So I got so far所以我到目前为止

sed ‘s/\\/\\\\/g’

But I'm not sure how to add ignore但我不确定如何添加忽略

Sample:样本:

aa  \N  aa
\N  aa  aa
aa  aa  \N
\N  \N  aa
\N  aa  \N
aa  \N  \N
\N  \N  \N
aa  aa  aa
\N  bb  \N
\       aa

should be:应该:

aa  \N  aa
\N  aa  aa
aa  aa  \N
\N  \N  aa
\N  aa  \N
aa  \N  \N
\N  \N  \N
aa  aa  aa
\N  bb  \N
\\      aa

You could do:你可以这样做:

echo '\ \\ \N \\N' |
sed -E 's/\\(N)|(\\)/\\\1\2/g'
\\ \\\\ \N \\\N

You could try this 's/\/\([^N]\)/\/\/\1/'你可以试试这个's/\/\([^N]\)/\/\/\1/'

Simple match / and the character after, requiring to not match N character.简单匹配/和后面的字符,要求不匹配N字符。

The matched character (other from N ) will be stroed in captureing group and can be used in replacement with \1 :)匹配的字符(来自N的其他字符)将在捕获组中进行搜索,并可用于替换\1 :)

Results from sed.js.org: sed.js.org 的结果:

在此处输入图像描述

What you need is a negative lookahead , which is a way to say: I want to match a pattern that is not followed by something .你需要的是一个否定的lookahead ,这是一种表达方式:我想匹配一个没有被某些东西跟随的模式

As far as I know, sed does not support lookarounds, but you can keep most of your syntax with this perl command:据我所知, sed不支持环视,但您可以使用此perl命令保留大部分语法:

perl -pe 's/\\(?!N)/\\\\/g'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM