bash 如何使用 sed 在一行后插入换行符/换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17363030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:53:52  来源:igfitidea点击:

How do I insert a newline/linebreak after a line using sed

bashshellsedzsh

提问by realityloop

It took me a while to figure out how to do this, so posting in case anyone else is looking for the same.

我花了一段时间才弄清楚如何做到这一点,所以发布以防其他人正在寻找相同的内容。

回答by devnull

For adding a newline after a pattern, you can also say:

要在模式后添加换行符,您还可以说:

sed '/pattern/{G;}' filename

Quoting GNU sed manual:

引用GNU sed 手册

G
    Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.

EDIT:

编辑:

Incidentally, this happens to be covered in sed one liners:

顺便说一句,这恰好包含在sed one liners 中

 # insert a blank line below every line which matches "regex"
 sed '/regex/G'

回答by realityloop

This sed command:

这个 sed 命令:

sed -i '' '/pid = run/ a\
\
' file.txt

Finds the line with: pid = run

查找具有以下内容的行:pid = run

file.txt before

之前的文件.txt

; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid

; Error log file

and adds a linebreak after that line inside file.txt

并在 file.txt 中的那一行之后添加一个换行符

file.txt after

文件.txt之后

; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid


; Error log file

Or if you want to add text and a linebreak:

或者,如果您想添加文本和换行符:

sed -i '/pid = run/ a\
new line of text\
' file.txt

file.txt after

文件.txt之后

; Note: the default prefix is /usr/local/var
; Default Value: none
;pid = run/php-fpm.pid
new line of text

; Error log file

回答by Richard

A simple substitution works well:

一个简单的替换效果很好:

sed 's/pattern.*$/&\n/'

Example :

例子 :

$ printf "Hi\nBye\n" | sed 's/H.*$/&\nJohn/'
Hi
John
Bye

To be standard compliant, replace \n by backslash newline :

要符合标准,请将 \n 替换为反斜杠换行符:

$ printf "Hi\nBye\n" | sed 's/H.*$/&\
> John/'
Hi
John
Bye

回答by nemo

sed '/pattern/a\r' file name 

It will add a return after the pattern while gwill replace the pattern with a blank line.

它将在模式后添加一个返回,同时g将用空行替换模式。

If a new line (blank) has to be added at end of the file use this:

如果必须在文件末尾添加新行(空白),请使用以下命令:

sed '$a\r' file name

回答by Gerald Schade

Another possibility, e.g. if You don't have an empty hold register, could be:

另一种可能性,例如,如果您没有空的保留寄存器,则可能是:

sed '/pattern/{p;s/.*//}' file

Explanation:
/pattern/{...}= apply sequence of commands, if line with pattern found,
p= print the current line,
;= separator between commands,
s/.*//= replace anything with nothing in the pattern register,
then automatically print the empty pattern register as additional line)

说明:
/pattern/{...}= 应用命令序列,如果找到具有模式的行,
p= 打印当前行,
;= 命令之间的分隔符,
s/.*//= 用模式寄存器中的任何内容替换任何内容,
然后自动将空模式寄存器打印为附加行)