在 sed 中插入换行符 (Mac OS X) [英] Insert linefeed in sed (Mac OS X)

查看:263
本文介绍了在 sed 中插入换行符 (Mac OS X)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 sed 的替换部分插入换行符?

How do I insert a newline in the replacement part of sed?

此代码无效:

sed "s/(1234)/
1/g" input.txt > output.txt

input.txt 在哪里:

where input.txt is:

test1234foo123bar1234

和 output.txt 应该是:

and output.txt should be:

test
1234foo123bar
1234

但是我得到了这个:

testn1234foo123barn1234

注意:

这个问题专门针对 Mac OS X 版本的sed",社区已经注意到它的行为与 Linux 版本不同.

This question is specifically about the Mac OS X version of "sed", and the community has noted that it behaves differently than, say, Linux versions.

推荐答案

您的 sed 版本显然不支持 RHS(替换的右侧)中的 .您应该阅读 Eric Pement 维护的SED 常见问题,以选择一种可能的解决方案.我建议先尝试插入文字换行符.

Your sed version apparently does not support in RHS (right-hand side of substitution). You should read THE SED FAQ maintained by Eric Pement to choose one of possible solutions. I suggest trying first inserting literal newline character.

下面是它的引述.

4.1.如何在替换的 RHS 中插入换行符?

几个版本的 sed 允许 直接输入到 RHS 中,然后在输出时将其转换为换行符:ssed、gsed302a+、gsed103(使用 -x switch)、sed15+、sedmod 和 UnixDOS sed.最简单的解决方案是使用这些版本之一.

Several versions of sed permit to be typed directly into the RHS, which is then converted to a newline on output: ssed, gsed302a+, gsed103 (with the -x switch), sed15+, sedmod, and UnixDOS sed. The easiest solution is to use one of these versions.

对于其他版本的 sed,请尝试以下方法之一:

For other versions of sed, try one of the following:

(a) 如果从 Bourne shell 键入 sed 脚本,如果脚本使用单引号"或两个反斜杠 \,请使用一个反斜杠 如果脚本需要双引号".在下面的示例中,请注意第 2 行的前导 > 是由 shell 生成的,用于提示用户进行更多输入.用户输入斜杠、单引号,然后按 ENTER 终止命令:

(a) If typing the sed script from a Bourne shell, use one backslash if the script uses 'single quotes' or two backslashes \ if the script requires "double quotes". In the example below, note that the leading > on the 2nd line is generated by the shell to prompt the user for more input. The user types in slash, single-quote, and then ENTER to terminate the command:

 [sh-prompt]$ echo twolines | sed 's/two/& new
 >/'
 two new
 lines
 [bash-prompt]$

(b) 在脚本中使用带有一个反斜杠 的脚本文件,后面紧跟一个换行符.这会将换行符嵌入到替换"部分.示例:

(b) Use a script file with one backslash in the script, immediately followed by a newline. This will embed a newline into the "replace" portion. Example:

 sed -f newline.sed files

 # newline.sed
 s/twolines/two new
 lines/g

某些版本的 sed 可能不需要尾部反斜杠.如果是这样,请将其删除.

Some versions of sed may not need the trailing backslash. If so, remove it.

(c) 插入一个未使用的字符并通过 tr 管道输出:

(c) Insert an unused character and pipe the output through tr:

 echo twolines | sed 's/two/& new=/' | tr "=" "
"   # produces
 two new
 lines

(d) 使用 G 命令:

G 在模式空间的末尾附加一个换行符,加上保持空间的内容.如果保留空间为空,则无论如何都会附加换行符.换行符作为 存储在模式空间中,可以通过对 (...) 进行分组来寻址,并在 RHS 中移动.因此,要更改之前使用的双线"示例,以下脚本将起作用:

G appends a newline, plus the contents of the hold space to the end of the pattern space. If the hold space is empty, a newline is appended anyway. The newline is stored in the pattern space as where it can be addressed by grouping (...) and moved in the RHS. Thus, to change the "twolines" example used earlier, the following script will work:

 sed '/twolines/{G;s/(two)(lines)(
)/132/;}'

(e) 插入整行,而不是分行:

(e) Inserting full lines, not breaking lines up:

如果不改变行而只是在模式之前或之后插入完整的行,则该过程要容易得多.使用 i(插入)或 a(追加)命令,通过外部脚本进行更改.在匹配正则表达式的每一行之前插入 This line is new:

If one is not changing lines but only inserting complete lines before or after a pattern, the procedure is much easier. Use the i (insert) or a (append) command, making the alterations by an external script. To insert This line is new BEFORE each line matching a regex:

 /RE/i This line is new               # HHsed, sedmod, gsed 3.02a
 /RE/{x;s/$/This line is new/;G;}     # other seds

以上两个示例旨在作为从控制台输入的单行"命令.如果使用 sed 脚本,i 后紧跟文字换行符将适用于所有版本的 sed.此外,命令 s/$/This line is new/ 只有在保持空间已经为空(默认情况下)时才有效.

The two examples above are intended as "one-line" commands entered from the console. If using a sed script, i immediately followed by a literal newline will work on all versions of sed. Furthermore, the command s/$/This line is new/ will only work if the hold space is already empty (which it is by default).

在匹配正则表达式的每一行之后追加这一行是新的:

To append This line is new AFTER each line matching a regex:

 /RE/a This line is new               # HHsed, sedmod, gsed 3.02a
 /RE/{G;s/$/This line is new/;}       # other seds

在匹配正则表达式的每一行后附加 2 个空行:

To append 2 blank lines after each line matching a regex:

 /RE/{G;G;}                    # assumes the hold space is empty

用 5 个空行替换与正则表达式匹配的每一行:

To replace each line matching a regex with 5 blank lines:

 /RE/{s/.*//;G;G;G;G;}         # assumes the hold space is empty

(f) 如果可能,请使用 y/// 命令:

(f) Use the y/// command if possible:

在某些 Unix 版本的 sed(不是 GNU sed!)上,虽然 s/// 命令不会接受 RHS 中的 >y/// 命令可以.如果您的 Unix sed 支持它,则可以在 aaa 之后插入一个换行符(这不能移植到 GNU sed 或其他 sed):

On some Unix versions of sed (not GNU sed!), though the s/// command won't accept in the RHS, the y/// command does. If your Unix sed supports it, a newline after aaa can be inserted this way (which is not portable to GNU sed or other seds):

 s/aaa/&~/; y/~/
/;    # assuming no other '~' is on the line!

这篇关于在 sed 中插入换行符 (Mac OS X)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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