Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
Lily0756
V2EX  ›  Linux

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

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

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

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