V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Lily0756
V2EX  ›  Linux

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

  •  
  •   Lily0756 · 2018-06-14 11:44:01 +08:00 · 2058 次点击
    这是一个创建于 2114 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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$' 并不成功

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

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

    当使用管道接收命令输出时,管道具有缓冲 BUFF,而输出信息并无换行,故一直会阻塞直管道 BUFF 满以后右边的命令才会接收到数据;
    araraloren
        8
    araraloren  
       2018-06-14 16:41:27 +08:00
    我记得 cp 这个命令就是这样的,可以使用 stdbuf 这个命令
    chunk
        9
    chunk  
       2018-06-14 17:23:03 +08:00 via Android
    nc 端口 3>&1 1>&2 2>&3 3>&– >/dev/null | sed 's/\$/\$\n/g' | grep …
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1037 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:23 · PVG 06:23 · LAX 15:23 · JFK 18:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.