windows GNU sed - 查找或替换空格或新行。为什么这不起作用?v3.02 与 v4.2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3331785/
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
GNU sed - find or replacing spaces or new lines. Why is this not working? v3.02 vs v4.2
提问by barlop
C:\crp\cnp>sed -V
GNU sed version 3.02
Copyright (C) 1998 Free Software Foundation, Inc.......
C:\crp\cnp>type f.f
a a a
a a a
Trying to replace 'a' with spaces.
试图用空格替换“a”。
C:\crp\cnp>type f.f | sed -e s/a/\d032/g
d032 d032 d032
d032 d032 d032
why isn't it working?
为什么它不起作用?
I don't mind whether i'm finding or replacing spaces or new lines.. I just want to be able to specify them. It doesn't seem to be working and I don't know why.
我不介意我是在查找还是替换空格或新行..我只想能够指定它们。它似乎不起作用,我不知道为什么。
(Replacing spaces or a space, with f, doesn't work)
(用 f 替换空格或空格不起作用)
C:\crp\cnp>echo a a | sed s/\d32/f/
a a
Note- it seems it might work in 4.2 , But i'm interested in 3.02 'cos that's the version bundled with unxutils http://unxutils.sourceforge.net/
注意 - 它似乎可以在 4.2 中工作,但我对 3.02 'cos 感兴趣,这是与 unxutils http://unxutils.sourceforge.net/捆绑的版本
Update to question- thanks for paxdiablo's tip.. about gnu32win, I am now using that instead of unxutils. It is more up to date. I can now specify spaces. And tip of ghostdog, and paxdiablo, I see about the double quotes. I am fine specifying spaces with \d(since using 4.2) or with a space. But, I still can't remove new lines
更新问题 - 感谢 paxdiablo 的提示.. 关于 gnu32win,我现在使用它而不是 unxutils。它是最新的。我现在可以指定空格。以及 ghostdog 和 paxdiablo 的提示,我看到了双引号。我可以用 \d(因为使用 4.2)或空格来指定空格。但是,我仍然无法删除新行
C:\crp>type f.f | sed -e "s/\r\n/f/g"
C:\crp> 输入 ff | sed -e "s/\r\n/f/g"
a aa
啊啊啊
b bb
BB
c cc
cc
C:\crp>type f.f | sed -e "s/\d013\d010/f/g"
C:\crp> 输入 ff | sed -e "s/\d013\d010/f/g"
a aa
啊啊啊
b bb
BB
c cc
cc
C:\crp>type f.f | sed -e "s/\x0D\x0A/f/g"
C:\crp> 输入 ff | sed -e "s/\x0D\x0A/f/g"
a aa
啊啊啊
b bb
BB
c cc
cc
Note:This question was from 2010. Now it's 2020. Gnuwin32 is out of date(like the last time its Gnuwin32 sed was updated was 2010, with Sed 4.2.1 which was from 2009), Unxutils is even more out of date. So Gnuwin32 as of writing is a decade out of date, and Unxutils is more like 2 decades out of date, as of 2020. Cygwin is still kept up to date and as of writing is on Sed v 4.4 which is from 2017.
注意:这个问题是从 2010 年开始的。现在是 2020 年。Gnuwin32 已经过时了(就像它的 Gnuwin32 sed 上次更新是 2010 年,Sed 4.2.1 是从 2009 年开始的),Unxutils 更加过时。因此,截至 2020 年,Gnuwin32 已经过时了 10 年,而 Unxutils 更像是过时了 2 年。 Cygwin 仍然保持最新状态,截至撰写本文时,是在 2017 年的 Sed v 4.4 上。
回答by paxdiablo
Why aren't you just using a space character itself rather than some funny encoding? As in:
为什么不使用空格字符本身而不是一些有趣的编码?如:
sed -e 's/a/ /g'
For what it's worth, the command you gave also fails to work in 4.2.1 but, if you add in the quotes, it doeswork. So I suggest you change it to:
值得一提的是,您提供的命令在 4.2.1 中也无法使用,但是,如果您添加引号,它确实可以工作。所以我建议你改成:
sed -e 's/a/\d032/g'
Apologies, I've just noticed you're running Windows so you've probably got CygWin or GnuWin32 (or equivalent).
抱歉,我刚刚注意到您正在运行 Windows,所以您可能已经安装了 CygWin 或 GnuWin32(或同等版本)。
Quotes work differently under Windows so you should try two things. The first is to use "
instead of '
quotes:
引号在 Windows 下的工作方式不同,因此您应该尝试两件事。第一种是使用"
而不是'
引号:
sed -e "s/a/ /g"
Otherwise, the escape character in Windows is ^
so something like this should be able to escape the space:
否则,Windows 中的转义字符^
应该可以转义空格:
sed -e s/a/^ /g
As an aside, I'd be looking to switch to GnuWin32, if possible, which has more recent versions of
sed
(for example). It doesn't look like UnxUtils has had an update since 2003 based on that web page you link to. You can get individual packages from here. You're looking forcoreutils
which contains the bulk of the UNIX text processing toolkit.
顺便说一句,如果可能的话,我希望切换到 GnuWin32,它有更新的版本
sed
(例如)。根据您链接到的网页,UnxUtils 似乎自 2003 年以来没有进行过更新。您可以从这里获得单独的包裹。您正在寻找coreutils
其中包含大量 UNIX 文本处理工具包。
But, if you're stuck with UnxUtils, I'd just use the actual space rather than a decimal code, and then I'd use tr
to get rid of new lines:
但是,如果您坚持使用 UnxUtils,我将只使用实际空格而不是十进制代码,然后我将使用它tr
来摆脱新行:
tr -d "\n"
assuming of course that the tr
in textutils
can handle that syntax :-)
当然假设tr
intextutils
可以处理该语法:-)
回答by gkolarov
I stuck with the same problem on Win XP and double quotes didn't work when trying to print new line "\n". The solution was to use the new UnxUpdates.zip from http://unxutils.sourceforge.net/It works correct with "\n".
我在 Win XP 上遇到了同样的问题,并且在尝试打印新行“\n”时双引号不起作用。解决方案是使用来自http://unxutils.sourceforge.net/的新 UnxUpdates.zip 它与“\n”一起工作正常。
Sed version states: GNU sed version 4.0.7
sed 版本说明:GNU sed 版本 4.0.7
回答by ghostdog74
On windows, use double quotes
在 Windows 上,使用双引号
sed "s/a/\d032/g" file
or just
要不就
sed "s/a/ /g" file