views:

47

answers:

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

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

+2  A: 

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 does work. So I suggest you change it to:

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).

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

sed -e "s/a/ /g"

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

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

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"

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

paxdiablo
the command you gave does not work in 3.02C:\crp\cnp>echo a a | sed -e 's/a/ g' GIVES ERRORsed: -e expression #1, char 1: Unknown command: ``'''
barlop
And that second command, with the \d032 where you said it doesn't work in 4.2.1 but would if you put quotes. I find it does work in 4.2 and doesn't when I do as you did and put quotes whether `(backquote) or '(single quote).
barlop
@barlop, you appear to have missed the / before the g. But in any case, I've just noticed you're using Windows which is a slightly different beast in terms of shell, so see the update.
paxdiablo
The "s/a/ /g" example worked.. though any idea why the \d032 one isn't working in v3.02? (it does in v4.2, with or without quotes). In v3.02 C:\crp\cnp>echo a a | sed -e "s/a/\d032/g" GIVESd032 d032
barlop
No idea, maybe `\d` was introduced after 3.02. Try `\x20` and see if that works.
paxdiablo
And also, I don't see how to remove new lines. A reason why I wanted to use \d032 to specify space, was because I can specify any chars, including for example, new lines, which was one of the things I want to try to remove.
barlop
Well.. maybe i'll just download unxutils then install sed 4.2 and not use sed 3.02
barlop
I'd like to be able to specify new lines in sed.. and remove them or replace them with something. It is more powerful than TR. See update to question, has examples that don't work
barlop
OK.. I found this one.. explaining why SED has problems with new lineshttp://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n And according to this next link,EOF is another one(perhaps for windows only). So special characters / control characters generally are or can be an issuehttp://sourceforge.net/projects/gnuwin32/forums/forum/74807/topic/1599399So, it's something sed isn't good for, and people use TR or TR and SED. (and possibly cat before the TR, though cat may always be unnecessary).
barlop
also, as mentioned.. paxdiaglo's useful tip.. to use gnuwin32, not unxutils, unxutils is old - old versions of the programs. gnuwin32 almost certainly has all that unxutils has.
barlop
A: 

On windows, use double quotes

sed "s/a/\d032/g" file

or just

sed "s/a/ /g" file
ghostdog74
very right about those double quotes!second one works great. But first one (which may not require quotes), doesn't work in v3.02C:\crp\cnp>echo a a | sed -e "s/a/\d032/g" GIVESd032 d032(though works fine in 4.2 with or without quotes). Seems to fail in v3.02
barlop