sed 命令的奇怪行为
strange behaviour of sed command
谁能解释一下 sed
的这种行为?
cat -bash-3.2# cat tmp7
echo "this is the song that never ends
yes, it goes on and on, my friend
some people started singing it
not knowing what it was
and they'll continue singing it forever
just because..." >
-bash-3.2# sed -n 's_.*some\(.*\)started.*__p' tmp7
输出
people
-bash-3.2# cat tmp8
echo "this is the song that never endsyes, it goes on and on, my friendsome people started singing itnot knowing what it wasand they'll continue singing it foreverjust because..." >
sed -n 's_.*some\(.*\)started.*__p' tmp8
什么都不输出
我希望第二个命令的输出应该与第一个命令相同。
文件tmp7
和tmp8
的内容是一样的,唯一的区别是tmp7包含换行符而tmp8没有。
更新
尝试使用不同版本的 sed /usr/xpg4/bin/sed
,获得了理想的输出,但出现警告 sed: Missing newline at end of file tmp8.
。我想要没有警告的输出。
/usr/xpg4/bin/sed -n 's_.*some\(.*\)started.*__p' tmp8
sed: Missing newline at end of file tmp8.
people
Solaris 默认 sed
忽略最后一行不破坏现有脚本,因为在原始 Unix 实现中要求一行以新行终止。
GNU sed
有更宽松的行为,POSIX 实现接受事实但输出警告。您可以将 stderr 重定向到 /dev/null 以忽略它。
/usr/xpg4/bin/sed -n 's_.*some\(.*\)started.*__p' tmp8 2>/dev/null
谁能解释一下 sed
的这种行为?
cat -bash-3.2# cat tmp7
echo "this is the song that never ends
yes, it goes on and on, my friend
some people started singing it
not knowing what it was
and they'll continue singing it forever
just because..." >
-bash-3.2# sed -n 's_.*some\(.*\)started.*__p' tmp7
输出
people
-bash-3.2# cat tmp8
echo "this is the song that never endsyes, it goes on and on, my friendsome people started singing itnot knowing what it wasand they'll continue singing it foreverjust because..." >
sed -n 's_.*some\(.*\)started.*__p' tmp8
什么都不输出
我希望第二个命令的输出应该与第一个命令相同。
文件tmp7
和tmp8
的内容是一样的,唯一的区别是tmp7包含换行符而tmp8没有。
更新
尝试使用不同版本的 sed /usr/xpg4/bin/sed
,获得了理想的输出,但出现警告 sed: Missing newline at end of file tmp8.
。我想要没有警告的输出。
/usr/xpg4/bin/sed -n 's_.*some\(.*\)started.*__p' tmp8
sed: Missing newline at end of file tmp8.
people
Solaris 默认 sed
忽略最后一行不破坏现有脚本,因为在原始 Unix 实现中要求一行以新行终止。
GNU sed
有更宽松的行为,POSIX 实现接受事实但输出警告。您可以将 stderr 重定向到 /dev/null 以忽略它。
/usr/xpg4/bin/sed -n 's_.*some\(.*\)started.*__p' tmp8 2>/dev/null