[ Linux ] 如何从以下这种形式的 standard output 中过滤结果?

2018-06-14 11:44:01 +08:00
 Lily0756

nc 192.168.1.1 端口 example:data1$example:data2$example:data3$example:data1$example:data2$example:data3$

192.168.1.1 端口不停循环返回 example:data1$,example:data2$,example:data3$ 想要过滤出 example:data1$,使用命令 nc 192.168.1.1 端口 | grep 'example:data$' 并不成功

2084 次点击
所在节点    Linux
9 条回复
Lax
2018-06-14 12:16:57 +08:00
grep 'example:data\$'
Lily0756
2018-06-14 12:26:40 +08:00
@Lax 上面忘打 实际加了跳脱符号 主要问题我觉得是每次返回的数据都不会换行
chunk
2018-06-14 13:17:03 +08:00
@Lily0756 | tr '$' '$\n' | grep ...?
Lily0756
2018-06-14 14:00:38 +08:00
@chunk 试了一下 nc 192.1.168.1 端口号 | tr '\$' '\n',现在数据换行打印并不带$符号。但是 | tr '$' '\n' | grep 'example' 就不行。
chunk
2018-06-14 14:54:57 +08:00
| sed 's/\$/\$\n/g' | grep …
ryd994
2018-06-14 15:02:39 +08:00
grep -o 不就好了
linyinma
2018-06-14 16:29:33 +08:00
问题其实是由 “管道缓冲 ” 引起并非能是管道后面命令能改变的

当使用命令 'nc 192.168.1.1 端口 ' 你能在终端看到连续打印,是因为终端属性是无阻塞的;

当使用管道接收命令输出时,管道具有缓冲 BUFF,而输出信息并无换行,故一直会阻塞直管道 BUFF 满以后右边的命令才会接收到数据;
araraloren
2018-06-14 16:41:27 +08:00
我记得 cp 这个命令就是这样的,可以使用 stdbuf 这个命令
chunk
2018-06-14 17:23:03 +08:00
nc 端口 3>&1 1>&2 2>&3 3>&– >/dev/null | sed 's/\$/\$\n/g' | grep …

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/463034

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX