在 MacOS 上用 sed 中的换行符替换逗号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10748453/
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
Replace comma with newline in sed on MacOS?
提问by WildBill
I have a file of id's that are comma separated. I'm trying to replace the commas with a new line. I've tried:
我有一个以逗号分隔的 id 文件。我正在尝试用新行替换逗号。我试过了:
sed 's/,/\n/g' file
but it is not working. What am I missing?
但它不起作用。我错过了什么?
回答by Prince John Wesley
Use tr
instead:
使用tr
来代替:
tr , '\n' < file
回答by Walter Mundt
Use an ANSI-C quoted string$'string'
使用ANSI-C 带引号的字符串$'string'
You need a backslash-escaped literal newline to get to sed.
In bash at least, $''
strings will replace \n
with a real newline, but then you have to double the backslash that sed will see to escape the newline, e.g.
您需要一个反斜杠转义的文字换行符才能进入 sed。至少在 bash 中,$''
字符串将替换\n
为真正的换行符,但是您必须将 sed 看到的反斜杠加倍以转义换行符,例如
echo "a,b" | sed -e $'s/,/\\n/g'
Note this will not work on all shells, but will work on the most common ones.
回答by Max Nanasy
sed 's/,/\
/g'
works on Mac OS X.
适用于 Mac OS X。
回答by nar8789
If your sed usage tends to be entirely substitution expressions (as mine tends to be), you can also use perl -pe
instead
如果您的SED的使用往往是完全替换表达式(如矿山往往是),你也可以使用perl -pe
替代
$ echo 'foo,bar,baz' | perl -pe 's/,/,\n/g'
foo,
bar,
baz
回答by Ramon Rey
This works on MacOS Mountain Lion (10.8), Solaris 10 (SunOS 5.10) and RHE Linux (Red Hat Enterprise Linux Server release 5.3, Tikanga)...
这适用于 MacOS Mountain Lion (10.8)、Solaris 10 (SunOS 5.10) 和 RHE Linux(Red Hat Enterprise Linux Server 5.3,Tikanga)...
$ sed 's/{pattern}/\^J/g' foo.txt > foo2.txt
... where the ^J
is done by doing ctrl+v+j. Do mind the \
before the ^J
.
...^J
通过执行ctrl+ v+来完成的地方j。不要介意\
之前^J
。
PS, I know the sed in RHEL is GNU, the MacOS sed is FreeBSD based, and although I'm not sure about the Solaris sed, I believe this will work pretty much with any sed. YMMV tho'...
PS,我知道 RHEL 中的 sed 是 GNU,MacOS sed 是基于 FreeBSD 的,虽然我不确定 Solaris sed,但我相信这几乎适用于任何 sed。YMMV tho'...
回答by Ramon Rey
Apparently \r
is the key!
显然\r
是关键!
$ sed 's/, /\r/g' file3.txt > file4.txt
Transformed this:
改成这样:
ABFS, AIRM, AMED, BOSC, CALI, ECPG, FRGI, GERN, GTIV, HSON, IQNT, JRCC, LTRE,
MACK, MIDD, NKTR, NPSP, PME, PTIX, REFR, RSOL, UBNT, UPI, YONG, ZEUS
To this:
对此:
ABFS
AIRM
AMED
BOSC
CALI
ECPG
FRGI
GERN
GTIV
HSON
IQNT
JRCC
LTRE
MACK
MIDD
NKTR
NPSP
PME
PTIX
REFR
RSOL
UBNT
UPI
YONG
ZEUS
回答by Cnetwork
MacOS is different, there is two way to solve this problem with sed in mac
MacOS 不一样,在 mac 中用 sed 有两种方法可以解决这个问题
first ,use
\'$'\n''
replace\n
, it can work in MacOS:sed 's/,/\'$'\n''/g' file
the second, just use an empty line:
sed 's/,/\ /g' file
Ps. Pay attention the range separated by
'
the third, use gnu-sed replace the mac-sed
首先,使用
\'$'\n''
replace\n
,它可以在 MacOS 中工作:sed 's/,/\'$'\n''/g' file
第二,只使用一个空行:
sed 's/,/\ /g' file
附言。注意由分隔的范围
'
三、使用gnu-sed替换mac-sed
回答by ryenus
To make it complete, this also works:
为了使其完整,这也有效:
echo "a,b" | sed "s/,/\$(echo -e '\n\r')/"
回答by user2300875
Though I am late to this post, just updating my findings. This answer is only for Mac OS X.
虽然我这篇文章迟到了,只是更新我的发现。此答案仅适用于Mac OS X。
$ sed 's/new/
> /g' m1.json > m2.json
sed: 1: "s/new/
/g": unescaped newline inside substitute pattern
In the above command I tried with Shift+Enter to add new line which didn't work. So this time I tried with "escaping" the "unescaped newline" as told by the error.
在上面的命令中,我尝试使用 Shift+Enter 添加新行,但没有用。所以这次我尝试按照错误提示“转义”“未转义的换行符”。
$ sed 's/new/\
> /g' m1.json > m2.json
Worked! (in Mac OS X 10.9.3)
工作了!(在 Mac OS X 10.9.3 中)
回答by t3az0r
Just to clearify: man-page of sed on OSX (10.8; Darwin Kernel Version 12.4.0) says:
只是为了澄清:OSX 上的 sed 手册页(10.8;达尔文内核版本 12.4.0)说:
[...]
[...]
Sed Regular Expressions
Sed 正则表达式
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.
[...]
[...]
so I guess one have to use tr - as mentioned above - or the nifty
所以我想必须使用 tr - 如上所述 - 或者漂亮的
sed "s/,/^M
/g"
note: you have to type <ctrl>-v,<return>to get '^M' in vi editor
注意:你必须在 vi 编辑器中输入 < ctrl>-v,< return>才能得到 '^M'