如何用sed替换多个模式

时间:2022-03-23 22:27:52

This is my file example.txt

这是我的文件示例。

{foo}
{bar}
{f}oo}
{ba{r}
{fo}o}
{b{ar}

I want this result:

我希望这个结果:

<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>

i have command in notepad++ to get result as above

我在记事本++ +中有命令获得如上的结果

([}]\r\n)|(\r\n[{])|(^[{])|([}]$)
(?1}</div>)(?2<div class="id">{)(?3<div class="id">{)(?4}</div>)

I tried with this sed command

我尝试过这个sed命令

sed -i 's@}\r\n@</div>@g' *.html

which left my file unchanged. How to do this correctly with sed?

这使我的文件保持不变。如何用sed正确地执行此操作?

4 个解决方案

#1


2  

Use the following sed command with specific regex pattern:

使用特定regex模式的下列sed命令:

sed -ri 's/\{(.+)\}/<div class="id">\1<\/div>/g' testfile

-r option allows extended regular expressions

-r选项允许扩展正则表达式


\{(.+)\} - matches any characters enclosed with curly braces {}

\{(.+)\}-匹配带有花括号{}的任何字符

\1 - points to the first captured group which is (.+)

\1 -指向第一个捕获的组(.+)

#2


1  

How about using two simple expressions, like this:

用两个简单的表达,比如:

sed -i '' -e 's/^./<div class="id">/;s/.$/<\/div>/' file
  • replaces the first character with <div class="id"> and the last character with </div> leaving the rest of the line intact.
  • 将第一个字符替换为
    ,最后一个字符替换为
    ,使行其余部分保持不变。
  • no need for any capture groups
  • 不需要任何捕获组

Gives this output:

给这个输出:

<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>

#3


0  

How about

如何

sed -e 's/{\(.*\)}/<div class="id">\1<\/div>/'

sed is line-oriented, so you won't find \n in a record (unless you're using some of the advanced commands).

sed是面向行的,因此您不会在记录中找到\n(除非您使用了一些高级命令)。

#4


0  

It may interest you to user Perl

您可能会对用户Perl感兴趣

$ cat file
{foo}
{bar}
{f}oo}
{ba{r}
{fo}o}
{b{ar}
$
$ perl -lpe 's/^.(.*?).$/<div class="id">\1<\/div>/' file
$ 
<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>

It also has save in place like sed with -i

它也像sed和i一样保存了下来。

#1


2  

Use the following sed command with specific regex pattern:

使用特定regex模式的下列sed命令:

sed -ri 's/\{(.+)\}/<div class="id">\1<\/div>/g' testfile

-r option allows extended regular expressions

-r选项允许扩展正则表达式


\{(.+)\} - matches any characters enclosed with curly braces {}

\{(.+)\}-匹配带有花括号{}的任何字符

\1 - points to the first captured group which is (.+)

\1 -指向第一个捕获的组(.+)

#2


1  

How about using two simple expressions, like this:

用两个简单的表达,比如:

sed -i '' -e 's/^./<div class="id">/;s/.$/<\/div>/' file
  • replaces the first character with <div class="id"> and the last character with </div> leaving the rest of the line intact.
  • 将第一个字符替换为
    ,最后一个字符替换为
    ,使行其余部分保持不变。
  • no need for any capture groups
  • 不需要任何捕获组

Gives this output:

给这个输出:

<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>

#3


0  

How about

如何

sed -e 's/{\(.*\)}/<div class="id">\1<\/div>/'

sed is line-oriented, so you won't find \n in a record (unless you're using some of the advanced commands).

sed是面向行的,因此您不会在记录中找到\n(除非您使用了一些高级命令)。

#4


0  

It may interest you to user Perl

您可能会对用户Perl感兴趣

$ cat file
{foo}
{bar}
{f}oo}
{ba{r}
{fo}o}
{b{ar}
$
$ perl -lpe 's/^.(.*?).$/<div class="id">\1<\/div>/' file
$ 
<div class="id">foo</div>
<div class="id">bar</div>
<div class="id">f}oo</div>
<div class="id">ba{r</div>
<div class="id">fo}o</div>
<div class="id">b{ar</div>

It also has save in place like sed with -i

它也像sed和i一样保存了下来。