English 中文(简体)
如果在开张前线有已知的字句,则使用原封顶取代曲线之间的模式
原标题:Using sed to replace a pattern between curly braces where line immediately before the opening curly brace has a known word

我要改变:

inlet
{
    type    patch;
    ...
}
outlet
{
        type  patch;
} 

纽约总部

inlet
{
    type    wall;
    ...
}
outlet
{
        type  patch;
} 

As a first attempt 纽约总部 achieve this, I tried 纽约总部 limit the range between inlet and outlet and replace "patch" with wall. echo "inlet { patch } outlet" | sed "/^(inlet [^ ]) . ([^ ]*)$/s//1 wall 2/" which gave output : inlet { wall outlet The last curly bracket is missing. I came up with this searching this forum and not quite sure of meaning of every command.

问题回答

Whether or not this solution is appropriate will depend on your actual data, but based on your example, one potential option is to use awk and change the record separator:

cat test.txt
inlet
{
    type    patch;
    ...
}
outlet
{
        type  patch;
}

awk  BEGIN{RS="^[[:alpha:]]"} /inlet/ {sub("patch", "wall", $0); print} !/inlet/  test.txt
inlet
{
    type    wall;
    ...
}
outlet
{
        type  patch;
}




相关问题
complex batch replacement linux

I m trying to do some batch replacement for/with a fairly complex pattern So far I find the pattern as: find ( -name *.php -o -name *.html ) -exec grep -i -n hello {} + The string I want ...

HTML anchor replace with RegEx

I have HTML data which I ll be using in a client app. I need to Regex.Replace the <a> tags from <a href="Bahai.aspx">Bahai</a> to <a href="#" onclick="process( Bahai.aspx );...

Replace placeholders with text - C?

I use the below function to retrieve characters one by one from a FILE stream. Now the text in that file will sometimes include places holders like !first_name! within the text. What s the best way to ...

Extract float from text line with sed?

I am issuing a sed replace on a line, trying to extract a particular floating-point value, but all that appears to be matched is the right-side of the decimal Text Line: 63.544: [GC 63.544: [DefNew: ...

热门标签