2011-05-05 315 views
4

当我将某些东西输入到不包含任何换行符的sed中时,几次注意到这一点,sed不执行。执行sed时不需要换行符中的换行符

实施例,流包含一些文本(无任何新行)

foo 

的命令:

$echo -n "foo" | sed 's/foo/bar/' 

输出什么。

而如果我添加一个换行符流的结束,上述将输出命令的预期替换:

$echo "foo" | sed 's/foo/bar/' 
bar 

我找到了很多,我没有的情况下,和真的不想在我的流中使用换行符,但我仍想运行sed替换。

任何想法如何做到这一点将不胜感激。

回答

1

你在什么环境?您使用什么sed

因为在这里,我有一个更好的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. 

注意的一种方式,以获得更好sed是把它作为类型perl -pe,如:

$ cat test.txt | perl -pe 's/foo/bar/' | hexdump -C 
00000000 62 61 72           |bar| 
00000003 
+0

尼斯一个丹尼尔。我不确定我有哪些sed,但我很确定它已经很老了。我在我的cygwin中尝试了相同的命令,它的工作原理是它必须是我使用的服务器上的旧版本。但是,你的perl -pe命令完美无缺,所以现在可以做得很好。好工作。 – Ben 2011-05-05 23:56:33

0

战略经济对话要知道什么时候输入完成,所以它看起来要么是一个文件结束或结束行,这是没有真正的解决方法..