如何替换文件中的一个段落? [英] How to substitute a paragraph in file?

查看:360
本文介绍了如何替换文件中的一个段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变比线的文件的更。这是一个CSS文件,我想换到另一个部分。

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:

    /*** Debugging ***/
    draw-button-bounding-rects: false;
    draw-button-rects: false;
    debug-touch-points: false;
    draw-reactive-areas: false;
}

MImAbstractKeyAreaStyle.Landscape {
    /*** Label Setttings ***/
    label-margin-top: 0.6mm;
    label-margin-left-with-secondary: -1; /* not used, labels are centered horizontally */
    secondary-label-separation: 0;

    /*** Behaviour ***/
    touchpoint-horizontal-gravity: 32px;
    touchpoint-vertical-gravity: 20px;

    /*** Key Geometry ***/
    key-height-small: -1; /* unused */
    key-height-medium: 46px;
    key-height-large: 66px;
    key-height-x-large: 82px;
    key-height-xx-large: -1; /* unused */

    key-width-small-fixed: -1; /* unused */
    key-width-medium-fixed: 76px;
    key-width-large-fixed: 93px;
    key-width-x-large-fixed: 178px;
    key-width-xx-large-fixed: 244px;
    key-width-stretched-fixed: -1; /* unused */

    key-margin-left: 4px;
    key-margin-top: 0px;
    key-margin-right: 4px;
    key-margin-bottom: 12px;

    padding-left: 11px;
    padding-top: 8px;
    padding-right: 11px;
    padding-bottom: 8px;

    /*** Key Area Geometry ***/
    size: 854 -1;
}

MImAbstractKeyAreaStyle.Portrait {

如果我用 SED -n'/aStyle.Landscape {/,/} / P'它打印正确的段落,但我想替换与另一个。我曾尝试与 sed的'/aStyle.Landscape {{/}/s/aStyle.Landscape /事/ 和类似的,但我失去了一些东西。

If I use sed -n '/aStyle.Landscape {/,/}/p' it prints the correct paragraph but I want to substitute that with another. I have tried with sed '/aStyle.Landscape {/}/s/aStyle.Landscape {/something/' and similar but I am missing something.

推荐答案

在显示 SED 语法并不完全准确;如果它的工作,那么你必须已经使用:

The sed syntax you show is not entirely accurate; if it worked, then you must have used:

sed -n '/aStyle.Landscape {/,/}/p'

从aStyle.Landscape'行打印到结束花。

to print the lines from 'aStyle.Landscape' to the closing brace.

如果您要更换的那款,那么你可以这样做:

If you want to replace that paragraph, then you can do:

sed '/aStyle.Landscape {/,/}/c\
MImAbstractKeyAreaStyle.Landscape {\
    /*** Label Setttings ***/\
    label-margin-top: 6.0mm;\
    label-margin-left-with-secondary: +1;\
    secondary-label-separation: 2;\
    ...\
}'

请注意,除了最后在​​替换文本每行后跟一个反斜杠。

Note that each line in the replacement text except the last is followed by a backslash.

您可能会发现它更好地使用脚本文件:

You might find it better to use a script file:

sed -f sed.script css.file


当我运行上面的脚本,从这个答案到一个文件逐字复制使用'XX',从问题的CSS片段(在'xx.css')SH XX< xx.css ,我得到的产量为:

    /*** Debugging ***/
    draw-button-bounding-rects: false;
    draw-button-rects: false;
    debug-touch-points: false;
    draw-reactive-areas: false;
}

MImAbstractKeyAreaStyle.Landscape {
    /*** Label Setttings ***/
    label-margin-top: 6.0mm;
    label-margin-left-with-secondary: +1;
    secondary-label-separation: 2;
    ...
}

MImAbstractKeyAreaStyle.Portrait {

您说您在一行上得到的一切......我不知道你是如何运行脚本?如果一切是在同一行',我不知道如果你是这样的:

You said you got everything on one line...I wonder how you ran the script? If everything was 'on one line', I wonder if you do something like:

output=$(sed ... file.css)

依次为:

echo $output

这将拉平一切一行(认为它是最小化)。在CSS的形状是preserved如果你写来代替:

This would flatten everything to one line (think of it as minimization). The shape of the CSS would be preserved if you wrote instead:

echo "$output"


这<一个href=\"http://stackoverflow.com/questions/8456136/how-do-i-set-new-line-with-a-sed-using-c\">question显示了扁平化的问题出现了。有壳间pretation(即运行命令外壳,壳是由指令),这是足以让任何人头疼。加倍的反斜杠可能就足够了,但我创建一个脚本并运行。


This question shows how the 'flattening' problem arose. There were two levels of shell interpretation (the shell that runs the su command, and the shell that is run by the su command), which is enough to give anyone a headache. Doubling up the backslashes might be sufficient, but I'd create a script and run that.

这篇关于如何替换文件中的一个段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆