Reputation: 6887
Have noticed this a fewtimes when I'm piping something into sed which does not contain any newlines, sed does not execute.
Example, a stream contains some text (without any newline)
foo
The command:
$echo -n "foo" | sed 's/foo/bar/'
Outputs nothing.
Whereas if I add a newline character to the end of the stream, The command above will output the expected replacement:
$echo "foo" | sed 's/foo/bar/'
bar
I've found a lot of situations where I don't have, and don't really want a newline character in my stream, but I still want to run a sed replacement.
Any ideas how this can be done would be appreciated.
Upvotes: 4
Views: 2071
Reputation: 3066
sed has to know when the input is done, so it looks for either an End Of File or an End Of Line, there isn't really a way around this..
Upvotes: 1
Reputation: 23548
What environment are you in? What sed
are you using?
Because here, I've got a better sed
:
$ echo -n foo | sed 's/foo/bar/'
bar$
$ echo -n foo > test.txt
$ hexdump -C test.txt
00000000 66 6f 6f |foo|
00000003
$ cat test.txt | sed 's/foo/bar/'
bar$ cat test.txt | sed 's/foo/bar/' | hexdump -C
00000000 62 61 72 |bar|
00000003
$ sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
Note that one way to get a better sed
is to type it as perl -pe
, as in:
$ cat test.txt | perl -pe 's/foo/bar/' | hexdump -C
00000000 62 61 72 |bar|
00000003
Upvotes: 1