使用 sed 的 append/change/insert 没有换行符 [英] Using sed's append/change/insert without a newline
问题描述
我想在 SED 中替换我的模式空间.我可以用 s/^.*$/hello world/;
做到这一点 - 但我可以使用 c
命令以某种方式做到这一点 - 在我的 sed 脚本中不使用换行符?我不完全清楚这是否有可能.
I want to replace my pattern space in SED. I can do this with s/^.*$/hello world/;
- but can I do it using the c
command somehow - without using line breaks in my sed script? It's not entirely clear to me whether that's possible in any way.
(a
和 i
命令的相同问题)
(Same question for the a
and i
commands)
推荐答案
如果你的 shell 是 bash,这里有一种在单行中使用 c
的便捷方法:
If your shell is bash, here is a convenient way to use c
in a one-liner:
$ seq 3 | sed $'/2/c\\\nNew Text'
1
New Text
3
这将查找任何包含 2
的行并将其更改为 New Text
.
This looks for any line containing 2
and changes it to New Text
.
这使用 bash 的 $'...'
功能在字符串中输入换行符.换行符由 \n
表示.c
后面需要的反斜杠用\\
表示.
This uses bash's $'...'
feature to enter a newline in a string. The newline is represented by \n
. The backslash that is needed after the c
is represented by \\
.
$'...'
功能在 ksh93
、zsh
、mksh
和FreeBSD sh
.
The $'...'
feature is also available in ksh93
, zsh
, mksh
, and FreeBSD sh
.
这篇关于使用 sed 的 append/change/insert 没有换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!