Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用sed替换两个匹配模式之间的所有行(在OSX BSD上)_Regex_Macos_Shell_Sed_Bsd - Fatal编程技术网

Regex 如何使用sed替换两个匹配模式之间的所有行(在OSX BSD上)

Regex 如何使用sed替换两个匹配模式之间的所有行(在OSX BSD上),regex,macos,shell,sed,bsd,Regex,Macos,Shell,Sed,Bsd,我想知道如何: 替换两个匹配模式之间的所有行(不包括模式-独占)。请注意,这些将在单独的行中 替换两个匹配模式(包括)之间的所有行。请注意,这些将在单独的行中 我已经开始了第一个,但它没有产生预期的结果(实际上到目前为止没有结果)。请记住,这是针对Mac OSX(BSD)上的sed。本例中的模式是两个单独行上的html注释 我的shell脚本如下所示: REPLACEWITH="Replacement text here" sed -i '' -e "s&\(<!--Begin

我想知道如何:

  • 替换两个匹配模式之间的所有行(不包括模式-独占)。请注意,这些将在单独的行中

  • 替换两个匹配模式(包括)之间的所有行。请注意,这些将在单独的行中 我已经开始了第一个,但它没有产生预期的结果(实际上到目前为止没有结果)。请记住,这是针对Mac OSX(BSD)上的sed。本例中的模式是两个单独行上的html注释

我的shell脚本如下所示:

REPLACEWITH="Replacement text here"
sed -i '' -e "s&\(<!--BeginNotes-->\).*\(<!--EndNotes-->\)&\1$REPLACEWITH\2&" /Users/BlaNameHere/builds/development/index.php
REPLACEWITH=“此处替换文本”
sed-i'-e“s&\(\).\(\)&\1$REPLACEWITH\2&“/Users/BlaNameHere/builds/development/index.php
在我的index.php文件的头部是以下摘录:

<!--BeginNotes-->
    <!--asdasd-->
    <script type="application/javascript">var Jaop;</script>
<!--EndNotes-->

var Jaop;
示例结果a

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <!--BeginNotes-->
        Replacement text here
        <!--EndNotes-->
    </head>
    <body>

    </body>
</html>

此处为替换文本
示例结果b

<!DOCTYPE html>
<html>
    <head>
        <title></title>
Replacement text here
    </head>
    <body>

    </body>
</html>

此处为替换文本
perl一行程序:

perl -i -0777 -pe 's/(<!--BeginNotes-->).*(<!--EndNotes-->)/$1'"$REPLACEWITH"'$2/s' index.php
sed-n:b
$ !{
N
b b
}
$ {
s |\(\).\(\n\)\([[:blank:]*\)\(\)\1\2\3${REPLACEWITH}\2\3\4|
P
}“/Users/BlaNameHere/builds/development/index.php

您需要将文件加载到缓冲区,因为sed逐行工作

您是绑定到sed,还是允许使用其他工具(例如awk)?我不介意看到awk解决方案,目前我对sed最为熟悉。我也希望保持标题的真实性。您希望文件在操作后是什么样子的?我将在上面添加示例文件a和b
perl -e '
    # read the file as a single string
    open $in, "<", "index.php";
    my $text;
    {
        local $/; 
        $text = <$in>;
    }
    close $in;

    # transform the string
    $text =~ s/(<!--BeginNotes-->).*(<!--EndNotes-->)/$1'"${REPLACEWITH}"'$2/s;

    # write out the new string
    open $out, ">", "index.php.tmp";
    print $out $text;
    close $out;

    # over-write the original file
    rename "index.php.tmp", "index.php"; 
'
sed -n ":b
$ !{
   N
   b b
   }
$ {
   s|\(<!--BeginNotes-->\).*\(\n\)\([[:blank:]]*\)\(<!--EndNotes-->\)|\1\2\3${REPLACEWITH}\2\3\4|
   p
   }" /Users/BlaNameHere/builds/development/index.php