2011-05-05 309 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

戰略經濟對話要知道什麼時候輸入完成,所以它看起來要麼是一個文件結束或結束行,這是沒有真正的解決方法..