问题
I am feeling stupid but have tried multiple ways of having new line in my script. I got help from Jonathan with a sed command. It worked great but the formatting is lost and now I can't find a way to make it work.
The code looks like this:
su -c "sed -i '/aStyle.Landscape {/,/}/c\
MImAbstractKeyAreaStyle.Landscape {\
/*** Label Setttings ***/\
label-margin-top: 10.6mm; /* 0.6 */\
label-margin-left-with-secondary: -1; /* not used, labels are centered horizontally */\
secondary-label-separation: 0;\
...
/*** Key Area Geometry ***/\
size: 854 -1;\
}' file.css"
I wanted to substitute a paragraph with another one. But with this command everything are printed on one line. I want it to keep the formatting. My original question is here: How to substitute a paragraph in file?
回答1:
First try without the su -c
. Between your ""
quotes, the escaping is different and make it much more complicated.
When you're done, either put the result in a script file and call that with su -c ./script.sh
, or adapt the escaping (but that's never gonna be nice).
回答2:
This might work for you:
echo hello | sed '/.*/c\This first line is no longer "hello"\nand this is the second line\nand this the third line'
This first line is no longer "hello"
and this is the second line
and this the third line
In GNU sed you can embed the newlines \n
but please see my amended answer to you last question (using a here-document) for a WYSIWYG answer.
来源:https://stackoverflow.com/questions/8456136/how-do-i-set-new-line-with-a-sed-using-c