GNU sed - 查找或替换空格或换行符。为什么这不工作? v3.02和v4.2 [英] GNU sed - find or replacing spaces or new lines. Why is this not working? v3.02 vs v4.2

查看:188
本文介绍了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

为什么不工作?

我不介意我是找到还是替换空格或换行我只想要能够指定它们。它似乎不工作,我不知道为什么。

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.

(用f替换空格或空格不起作用)

(Replacing spaces or a space, with f, doesn't work)

C:\crp\cnp>echo a a | sed s/\d32/f/
a a

注意 - 它似乎可能工作在4.2,但我对3.02'cos感兴趣,这是与unxutils捆绑的版本 http://unxutils.sourceforge.net/

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/

更新到问题 -
感谢paxdiablo的提示..关于gnu32win,我现在使用它而不是unxutils。它是更新的。我现在可以指定空格。和ghostdog的提示,和paxdiablo,我看到双引号。我很好用\d(因为使用4.2)或空格指定空格。
但是,我仍然无法删除新行

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/ \r\\\
/ f / g

C:\crp>type f.f | sed -e "s/\r\n/f/g"

a a

b bb

c cc

C:\ crp> type ff | sed -e/ \d013\d010 / f / g

C:\crp>type f.f | sed -e "s/\d013\d010/f/g"

a aa

b bb

c cc

C:\crp> type ff | sed -e/ \x0D\x0A / f / g

C:\crp>type f.f | sed -e "s/\x0D\x0A/f/g"

a a

b bb

c cc

推荐答案

一个空格字符本身而不是一些有趣的编码?如:

Why aren't you just using a space character itself rather than some funny encoding? As in:

sed -e 's/a/ /g'

对于值得的,你提供的命令也不能在4.2.1中工作,但是如果你添加在引号中, em>工作。因此,我建议您将其更改为:

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:

sed -e 's/a/\d032/g'






抱歉,我注意到您正在运行Windows 've可能有CygWin或GnuWin32(或等效的)。


Apologies, I've just noticed you're running Windows so you've probably got CygWin or GnuWin32 (or equivalent).

引用在Windows下工作不同,所以你应该尝试两件事。第一个是使用而不是'引号:

Quotes work differently under Windows so you should try two things. The first is to use " instead of ' quotes:

sed -e "s/a/ /g"

否则,Windows中的转义字符是 ^ ,所以这样的东西应该能够转义空格:

Otherwise, the escape character in Windows is ^ so something like this should be able to escape the space:

sed -e s/a/^ /g








另外,如果可能的话,我会寻找切换到GnuWin32,它有更新的版本 sed (例如)看起来UnxUtils看起来不像自2003年以来,您可以通过此处链接到该网页。找到 coreutils ,其中包含大部分UNIX文本处理工具包。

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 for coreutils which contains the bulk of the UNIX text processing toolkit.

但是,如果你坚持使用UnxUtils,我只是使用实际的空间而不是十进制代码,然后我会使用 tr 摆脱新行:

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:

tr -d "\n"

当然,假设 tr textutils 该语法: - )

assuming of course that the tr in textutils can handle that syntax :-)

这篇关于GNU sed - 查找或替换空格或换行符。为什么这不工作? v3.02和v4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆