请改用 tr
:
tr , '\n' < file
使用 ANSI-C quoted string $'string'
您需要一个反斜杠转义的文字换行符才能进入 sed。至少在 bash 中,$''
字符串会将 \n
替换为真正的换行符,但是您必须将 sed 看到的反斜杠加倍以转义换行符,例如
echo "a,b" | sed -e $'s/,/\\\n/g'
注意 this will not work on all shells,但适用于最常见的。
echo "a,b" | sed -e $'s/,/\\n/g
。
man bash
的“报价”部分。
sed -E $'s/<\\/br>/\\\n/g' file
,无需安装 gnu-sed
-e
部分似乎没有必要。
fzf
一起用于调试我的 $PATH 以防丢失:echo $PATH | sed -e $'s/:/\\\n/g' | fzf
^.^
sed 's/,/\
/g'
适用于 Mac OS X。
如果您的 sed 用法倾向于完全是替换表达式(就像我的倾向于那样),您也可以使用 perl -pe
代替
$ echo 'foo,bar,baz' | perl -pe 's/,/,\n/g'
foo,
bar,
baz
MacOS不同,有两种方法可以在mac中用sed解决这个问题
首先,用\'$'\n''替换\n,它可以在MacOS下工作:sed 's/,/\'$'\n''/g'文件
第二种,只用一个空行:sed 's/,/\ /g' file
附言。注意用'分隔的范围
三、用gnu-sed替换mac-sed
显然 \r
是关键!
$ sed 's/, /\r/g' file3.txt > file4.txt
改造了这个:
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
对此:
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
\r
与 \n
不同,这可能会破坏进一步的数据操作和使用。
\r
的答案中更清楚地说明!
这适用于 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
... 其中 ^J
是通过执行 ctrl+v+j 完成的。请注意 ^J
之前的 \
。
PS,我知道 RHEL 中的 sed 是 GNU,MacOS sed 是基于 FreeBSD 的,虽然我不确定 Solaris sed,但我相信这几乎适用于任何 sed。 YMMV 你...
为了使它完整,这也有效:
echo "a,b" | sed "s/,/\\$(echo -e '\n\r')/"
tr
解决方案,这已经是公认的答案。
\n\r
而不是 \r\n
?我尝试了 \r\n
,这是 Windows 上的必要顺序。但是,在我这样做之后,它会在 vim
中留下很多 ^M
回车字符,所以我认为它应该只有 \n
但单独的 \n
不起作用。
虽然我迟到了这篇文章,但只是更新了我的发现。此答案仅适用于 Mac OS X。
$ sed 's/new/
> /g' m1.json > m2.json
sed: 1: "s/new/
/g": unescaped newline inside substitute pattern
在上面的命令中,我尝试使用 Shift+Enter 添加不起作用的新行。因此,这次我尝试按照错误提示“转义”“未转义的换行符”。
$ sed 's/new/\
> /g' m1.json > m2.json
工作! (在 Mac OS X 10.9.3 中)
$ echo $PATH | sed -e $'s/:/\\\n/g'
/usr/local/sbin
/Library/Oracle/instantclient_11_2/sdk
/usr/local/bin
...
在莫哈韦为我工作
只是为了澄清:OSX(10.8;达尔文内核版本 12.4.0)上的 sed 手册页说:
[...]
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.
[...]
所以我想一个人必须使用 tr - 如上所述 - 或者 nifty
sed "s/,/^M
/g"
注意:您必须在 vi 编辑器中键入
macOS Mojave 上的 sed
于 2005 年发布,因此一种解决方案是安装 gnu-sed
,
brew install gnu-sed
然后使用 gsed
会如你所愿,
gsed 's/,/\n/g' file
如果您更喜欢 sed
,只需 brew info gnu-sed
建议的 sudo sh -c 'echo /usr/local/opt/gnu-sed/libexec/gnubin > /etc/paths.d/brew'
。重新启动您的术语,然后您在命令行中的 sed
是 gsed
。
FWIW,以下行在 Windows 中工作,并用换行符替换我的路径变量中的分号。我正在使用安装在我的 git bin 目录下的工具。
echo %path% | sed -e $'s/;/\\n/g' | less
我发现了另一个也在工作的命令。
find your_filename.txt -type f -exec sed -i 's/,/\n/g' {} \;
tr ',' ';' < input.csv > output.csv