繁体   English   中英

在 MacOS 上用换行符替换 sed 中的逗号?

[英]Replace comma with newline in sed on MacOS?

我有一个以逗号分隔的字符串文件。 我正在尝试用新行替换逗号。 我试过了:

sed 's/,/\n/g' file

但它不工作。 我错过了什么?

使用tr代替:

tr , '\n' < file

使用ANSI-C 引用的字符串$'string'

您需要一个反斜杠转义的文字换行符才能进入 sed。 至少在 bash 中, $''字符串将用真正的换行符替换\\n ,但是您必须将 sed 看到的反斜杠加倍以转义换行符,例如

echo "a,b" | sed -e $'s/,/\\\n/g'

请注意, 这不适用于所有外壳,但适用于最常见的外壳

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' file
  • 第二,只需使用一个空行:

     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

这适用于 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 tho'...

为了使其完整,这也有效:

echo "a,b" | sed "s/,/\\$(echo -e '\n\r')/"

虽然我这篇文章迟到了,只是更新我的发现。 此答案仅适用于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 上的 sed 手册页(10.8;达尔文内核版本 12.4.0)说:

[...]

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 - 如上所述 - 或者漂亮的

sed "s/,/^M
/g"

注意:您必须在 vi 编辑器中键入 < ctrl> -v,< return>才能获得 '^M'

macOS Mojave 上的sed于 2005 年发布,因此一种解决方案是安装gnu-sed

brew install gnu-sed

然后使用gsed就可以了,

gsed 's/,/\n/g' file

如果您更喜欢sed ,只需sudo sh -c 'echo /usr/local/opt/gnu-sed/libexec/gnubin > /etc/paths.d/brew' ,这是brew info gnu-sed建议的。 重新启动您的术语,然后您在命令行中的sedgsed

FWIW,以下行在 Windows 中工作,并用换行符替换我的路径变量中的分号。 我正在使用安装在 git bin 目录下的工具。

echo %path% | sed -e $'s/;/\\n/g' | less

我发现了另一个同样有效的命令。

find your_filename.txt -type f -exec sed -i 's/,/\n/g' {} \;

我们也可以在sed中做到这一点。

你可以尝试这个,

sed -i~bak 's/\,/\n/g' file

-i - 将更改文件中的修改。请谨慎使用此选项。

我希望这会对你有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM