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

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

问题描述

如何在sed的?更换部件插入新行

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

这code不工作:

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

其中,input.txt的是:

where input.txt is:

test1234foo123bar1234

和output.txt的应该是:

and output.txt should be:

test
1234foo123bar
1234

但insted的我得到这样的:

but insted I get this:

testn1234foo123barn1234

注意:

此问题是专门关于sed的Mac OS X的版本,和社会上指出,其行为比,不同比如说,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(取代右侧)N 。 href=\"http://sed.sourceforge.net/sedfaq.html\">战略经济对话回答问题通过埃里克Pement保持选择的可能的解决方案一个你应该阅读

Your sed version apparently does not support \n 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的许可证的几个版本的 \\ n 直接输入到RHS,然后将其转换为输出一个换行符:ssed,gsed302a +,gsed103(用 -x 开关),sed15 +,sedmod和UnixDOS sed的。最简单的解决方法是使用这些版本中的一个。

Several versions of sed permit \n 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:

(一)如果从Bourne shell中键入sed脚本,用一个反斜杠 \\ 如果脚本使用'单引号'两反斜线 \\\\ 如果脚本需要双引号。在下面的例子中,注意龙头> 2号线由外壳产生提示用户输入更多。在斜杠,单引号,然后在用户键入回车,终止命令:

(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) 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.

(三)插入一个未使用的字符和管道通过TR输出:

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

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

(四)使用命令:

(d) Use the G command:

摹追加一个换行符,加上保持空间模式空间的结尾的内容。如果保留空间是空的,一个换行符是无论如何追加。新行存储在模式空间为 \\ n 在那里可以通过分组 \\(... \\)而在RHS移动。因此,要改变以前使用的twolines例如,下面的脚本将工作:

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 \n 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\)\(\n\)/\1\3\2/;}'

(E)插入饱满的线条,没有违反排队:

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

如果一个不改变线路而在此之前或之后的模式只插入完整的生产线,这个过程要容易得多。使用 I (插入)或 A (追加)命令,通过外部脚本使得改变。要插入此行是新的在每行相匹配的正则表达式:

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 / $ /此行是新/ 如果保留空间已为空(这是默认设置)。只会工作

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).

要追加此行是新的每行匹配的正则表达式AFTER:

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)使用 ///Ÿ命令如果可能的话:

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

在sed的一些Unix版本(不是GNU sed的!),虽然取值/// 命令将不接受 \\ n 在RHS中, ///Ÿ命令一样。如果你的Unix Sed则支持吧,毕竟 AAA 换行符可以插入这种方式(这是不能移植到GNU SED或其他SEDS):

On some Unix versions of sed (not GNU sed!), though the s/// command won't accept \n 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/~/\n/;    # assuming no other '~' is on the line!

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

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