V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Lily0756
V2EX  ›  Linux

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

  •  
  •   Lily0756 · Jun 14, 2018 · 2742 views
    This topic created in 2886 days ago, the information mentioned may be changed or developed.

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

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

    当使用管道接收命令输出时,管道具有缓冲 BUFF,而输出信息并无换行,故一直会阻塞直管道 BUFF 满以后右边的命令才会接收到数据;
    araraloren
        8
    araraloren  
       Jun 14, 2018
    我记得 cp 这个命令就是这样的,可以使用 stdbuf 这个命令
    chunk
        9
    chunk  
       Jun 14, 2018 via Android
    nc 端口 3>&1 1>&2 2>&3 3>&– >/dev/null | sed 's/\$/\$\n/g' | grep …
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5436 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 07:09 · PVG 15:09 · LAX 00:09 · JFK 03:09
    ♥ Do have faith in what you're doing.