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

barlop
  • 12,887
  • 8
  • 80
  • 109

3 Answers3

6

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
  • 854,327
  • 234
  • 1,573
  • 1,953
  • the command you gave does not work in 3.02 C:\crp\cnp>echo a a | sed -e 's/a/ g' GIVES ERROR sed: -e expression #1, char 1: Unknown command: ``''' – barlop Jul 26 '10 at 01:57
  • 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 Jul 26 '10 at 01:59
  • @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 Jul 26 '10 at 02:13
  • 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" GIVES d032 d032 – barlop Jul 26 '10 at 02:19
  • No idea, maybe `\d` was introduced after 3.02. Try `\x20` and see if that works. – paxdiablo Jul 26 '10 at 02:33
  • 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 Jul 26 '10 at 02:33
  • Well.. maybe i'll just download unxutils then install sed 4.2 and not use sed 3.02 – barlop Jul 26 '10 at 02:41
  • 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 Jul 26 '10 at 03:18
  • OK.. I found this one.. explaining why SED has problems with new lines http://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 issue http://sourceforge.net/projects/gnuwin32/forums/forum/74807/topic/1599399 So, 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 Jul 26 '10 at 21:32
  • 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 Jul 27 '10 at 09:51
2

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

gkolarov
  • 21
  • 2
  • 1
    I use gnuwin32 now. unxutils is convenient to "install"(just a zip of binaries), if one is undesirably lazy but it's not worth it! gnuwin32>unxutils. unxutils is old. gnuwin32 has much more too. gnuwin32 has sed 4.2.1 so, newer than unxupdates.zip Also one time I had wget issues with unxutils, gnuwin32 was fine as newer version of wget. see my answer here for a bit more http://superuser.com/questions/168202/difference-between-unxutils-and-gnu-coreutils – barlop Dec 12 '12 at 12:33
0

On windows, use double quotes

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

or just

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