The regular expressions used in sed, by default, are basic regular expressions (BREs, see re_format(7) for more information), but extended
(modern) regular expressions can be used instead if the -E flag is given. In addition, sed has the following two additions to regular
expressions:
1. In a context address, any character other than a backslash (``\'') or newline character may be used to delimit the regular expression.
Also, putting a backslash character before the delimiting character causes the character to be treated literally. For example, in the
context address \xabc\xdefx, the RE delimiter is an ``x'' and the second ``x'' stands for itself, so that the regular expression is
``abcxdef''.
2. The escape sequence \n matches a newline character embedded in the pattern space. You cannot, however, use a literal newline charac-
ter in an address or in the substitute command.
12 回答
请改用
tr
:使用
$'string'
你需要一个反斜杠转义的文字换行符来获取sed . 至少在bash中,
$''
字符串将用真正的换行替换\n
,但是你必须加倍sed将看到的反斜杠以逃避换行符,例如适用于Mac OS X.
如果你的sed用法往往是完全替换表达式(就像我的那样),你也可以使用perl -pe
显然
\r
是关键!转化为:
对此:
这适用于MacOS Mountain Lion(10.8),Solaris 10(SunOS 5.10)和RHE Linux(Red Hat Enterprise Linux Server 5.3版,Tikanga)......
...通过ctrl v j完成
^J
. 请记住^J
之前的\
.PS,我知道RHEL中的sed是GNU,MacOS sed是基于FreeBSD的,虽然我不确定Solaris sed,但我相信这对任何sed都有用 . YMMV tho'......
为了完成它,这也有效:
MacOS不同,有两种方法可以用mac中的sed来解决这个问题
\'$'\n''
替换\n
,它可以在MacOS中工作:'
分隔的范围虽然我迟到了这篇文章,但只是更新我的发现 . 这个答案仅适用于 Mac OS X.
在上面的命令中,我尝试使用Shift Enter添加新行,但不起作用 . 所以这次我尝试“逃避”错误告诉的“未转义的换行符” .
Worked! (in Mac OS X 10.9.3)
只是为了澄清:OSX上的sed的man-page(10.8; Darwin内核版本12.4.0)说:
[...]
Sed正则表达式
[...]
所以我猜一个人必须使用tr - 如上所述 - 或者漂亮
注意:您必须在vi编辑器中键入<ctrl> -v,<return>才能获得'^M'
FWIW,以下行在Windows中工作,并用换行符替换路径变量中的分号 . 我正在使用我的git bin目录下安装的工具 .
我们也可以在sed中做到这一点 .
你可以试试这个,
-i - 将更改文件中的修改 . 请谨慎使用此选项 .
我希望这会对你有所帮助 .