GNU sed - 查找或替换空格或新行。为什么这不起作用? v3.02 与 v4.2
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
尝试用空格替换“a”。
C:\crp\cnp>type f.f | sed -e s/a/\d032/g
d032 d032 d032
d032 d032 d032
为什么它不工作?
我不介意是否查找或替换空格或新行..我只是希望能够指定它们。它似乎不起作用,我不知道为什么。
(用 f 替换空格或空格不起作用)
C:\crp\cnp>echo a a | sed s/\d32/f/
a a
注意-它似乎可以在 4.2 中工作,但我对 3.02 感兴趣,因为这是与 unxutils 捆绑在一起的版本 http://unxutils.sourceforge.net/
更新问题- 感谢 paxdiablo 的提示..关于 gnu32win,我现在使用它而不是 unxutils。它是最新的。我现在可以指定空格。还有 Ghostdog 和 Paxdiablo 的提示,我看到了双引号。我可以用 \d(自使用 4.2 以来)或空格指定空格。 但是,我仍然无法删除新行
C:\crp>type ff | sed -e "s/\r\n/f/g"
a aa
b bb
c cc
C:\crp>输入 ff | sed -e "s/\d013\d010/f/g"
a aa
b bb
c cc
C:\crp>输入 ff | sed -e "s/\x0D\x0A/f/g"
a aa
b bb
c cc
注意: 这个问题是 2010 年的。现在是 2020 年了。Gnuwin32 已经过时了(就像上次一样)它的Gnuwin32 sed更新是2010年,Sed 4.2.1是从2009年开始的),Unxutils更过时了。因此,截至撰写本文时,Gnuwin32 已经过时了 10 年,而截至 2020 年,Unxutils 更像是已经过时了 20 年。Cygwin 仍然保持最新状态,截至撰写本文时,Sed v 4.4 是从 2017 年开始的。
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.
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)
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/
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
C:\crp>type f.f | sed -e "s/\r\n/f/g"
a aa
b bb
c cc
C:\crp>type f.f | sed -e "s/\d013\d010/f/g"
a aa
b bb
c cc
C:\crp>type f.f | sed -e "s/\x0D\x0A/f/g"
a aa
b bb
c 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你不只使用空格字符本身而不是一些有趣的编码?正如:
就其价值而言,您给出的命令在 4.2.1 中也无法工作,但是,如果您添加引号,它确实可以工作。所以我建议您将其更改为:
抱歉,我刚刚注意到您正在运行 Windows,因此您可能已经安装了 CygWin 或 GnuWin32(或同等版本)。
引号在 Windows 下的工作方式有所不同,因此您应该尝试两件事。第一种是使用
"
而不是'
引号:否则,Windows 中的转义字符是
^
所以类似这样的东西应该能够转义空间:但是,如果您坚持使用 UnxUtils,我只会使用实际的空格而不是十进制代码,然后我会使用
tr
来删除新行:当然假设
textutils
中的tr
可以处理该语法:-)Why aren't you just using a space character itself rather than some funny encoding? As in:
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 does work. So I suggest you change it to:
Apologies, I've just noticed you're running Windows so you've probably got CygWin or GnuWin32 (or equivalent).
Quotes work differently under Windows so you should try two things. The first is to use
"
instead of'
quotes:Otherwise, the escape character in Windows is
^
so something like this should be able to escape the space: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:assuming of course that the
tr
intextutils
can handle that syntax :-)我在 Win XP 上遇到了同样的问题,并且在尝试打印新行“\n”时双引号不起作用。
解决方案是使用 http://unxutils.sourceforge.net/ 中的新 UnxUpdates.zip
它与“\n”一起工作正确。
Sed 版本指出:
GNU sed 版本 4.0.7
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".
Sed version states:
GNU sed version 4.0.7
在 Windows 上,使用双引号
或仅使用
On windows, use double quotes
or just