使用包罗万象的通配符查找和替换文本

Find and replace text with all-inclusive wild card

我有这样一个文件:

foo and more
stuff
various stuff
variable number of lines
with a bar
Stuff I want to keep
More stuff I want to Keep
These line breaks are important

我想替换 foo 和 bar 之间的所有内容,以便我得到:

foo testtext bar
Stuff I want to keep
More stuff I want to Keep
These line breaks are important

按照我尝试过的另一个线程中的建议: sed -e '/^foo/,/^bar/{/^foo/b;/^bar/{i testtext' -e 'b};d}' file.txt

是否有更通用的解决方案来查找和替换 foobar 之间的所有内容,绝对不管它是什么?

您可以使用以下 sed 脚本:

replace.sed:

# Check for "foo"
/\bfoo\b/    {   
    # Define a label "a"
    :a  
    # If the line does not contain "bar"
    /\bbar\b/!{
        # Get the next line of input and append
        # it to the pattern buffer
        N
        # Branch back to label "a"
        ba
    }   
    # Replace everything between foo and bar
    s/\(\bfoo\)\b.*\b\(bar\b\)/TEST DATA/
}

这样称呼它:

sed -f extract.sed input.file

输出:

fooTEST DATAbar
Stuff I want to keep
More stuff I want to Keep
These line breaks are important

如果您想使用 shell 脚本传递开始和结束定界符,您可以这样做(为简洁起见删除了注释):

#!/bin/bash

begin="foo"
end="bar"

replacement=" Hello world "

sed -r '/\b'"$begin"'\b/{
    :a;/\b'"$end"'\b/!{
        N;ba
    }
    s/(\b'"$begin"')\b.*\b('"$end"'\b)/'"$replacement"'/
}' input.file

只要 $start$end 不包含正则表达式特殊字符,上面的代码就可以工作, 使用下面的代码:

#!/bin/bash

begin="foo"
end="bar"
replace=" Helloworld "

# Escape variables to be used in regex
beginEsc=$(sed 's/[^^]/[&]/g; s/\^/\^/g' <<<"$begin")
endEsc=$(sed 's/[^^]/[&]/g; s/\^/\^/g' <<<"$end")
replaceEsc=$(sed 's/[&/\]/\&/g' <<<"$replace")

sed -r '/\b'"$beginEsc"'\b/{
    :a;/\b'"$endEsc"'\b/!{
        N;ba
    }
    s/(\b'"$beginEsc"')\b.*\b('"$endEsc"'\b)/'"$replaceEsc"'/
}' input.file