加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix – 执行sed而不需要流中的换行符

发布时间:2020-12-16 01:56:59 所属栏目:安全 来源:网络整理
导读:我已经注意到这几次当我将某些内容输入到不包含任何换行符的sed中时,sed不会执行. 例如,一个流包含一些文本(没有任何换行) foo 命令: $echo -n "foo" | sed 's/foo/bar/' 没有输出. 如果我在流的末尾添加换行符,则上面的命令将输出预期的替换: $echo "foo"
我已经注意到这几次当我将某些内容输入到不包含任何换行符的sed中时,sed不会执行.

例如,一个流包含一些文本(没有任何换行)

foo

命令:

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

没有输出.

如果我在流的末尾添加换行符,则上面的命令将输出预期的替换:

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

我发现了许多我没有的情况,并且我不想在我的流中使用换行符,但我仍然希望运行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: <bug-gnu-utils@gnu.org>.
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读