How to substitute a paragraph in file?

前端 未结 2 1644
栀梦
栀梦 2021-01-28 05:42

I want to change more than a line of a file. It is a CSS file and I want to swap out a section to another.

This is a snippet from the file:

    /*** Debu         


        
2条回答
  •  温柔的废话
    2021-01-28 05:57

    This might work for you to:

    cat <<\EOF > new_para.txt
    MImAbstractKeyAreaStyle.Landscape
        /*** Label Setttings ***
        label-margin-top: 6.0mm;
        label-margin-left-with-secondary: +1;
        secondary-label-separation: 2;
        ...
    }
    EOF
    > MImAbstractKeyAreaStyle.Landscape
    >     /*** Label Setttings ***
    >     label-margin-top: 6.0mm;
    >     label-margin-left-with-secondary: +1;
    >     secondary-label-separation: 2;
    >     ...
    > }
    sed -i -e '/aStyle.Landscape {/,/}/{/}/r new_para.txt' -e ';d}' file.css
    

    If the replacement paragraph is of the same length and your using GNU sed, this would work to:

    sed -i -e '/aStyle.Landscape {/,/}/{R new_para.txt' -e ';d}' file.css
    

    N.B. The last method only works for a single replacement, unless each paragraph to be replaced (in order) is in the new_para.txt file. This does give the user a chance to tailor each replacement paragraph, a subtle but powerful difference.

    EDIT:

    To achieve the same result without an intermediate file:

    cat <<\EOF | sed -i -e '/aStyle.Landscape {/,/}/{/}/r /dev/stdin ' -e ';d}' file.css
    MImAbstractKeyAreaStyle.Landscape
        /*** Label Setttings ***
        label-margin-top: 6.0mm;
        label-margin-left-with-secondary: +1;
        secondary-label-separation: 2;
        ...
    }
    EOF
    

提交回复
热议问题