AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1347113
Accepted
Cas
Cas
Asked: 2021-06-21 12:10:34 +0800 CST2021-06-21 12:10:34 +0800 CST 2021-06-21 12:10:34 +0800 CST

在 curl 请求后管道 grep 时,正则表达式的工作方式非常奇怪

  • 772

简单的问题,但真的很奇怪。

当我发出 curl 请求并执行... | grep -Po "^\d+$"时,即使有 400 多个结果应该匹配,它也不会返回任何内容。见下文:

#example snippet of curl output
107
00:15:54,936 --> 00:15:56,646
Yeah, this is him.
We got him.

108
00:16:07,823 --> 00:16:11,869
So, how long
you been in South Florida?

109
00:16:11,953 --> 00:16:13,871
A while.
Before that?

110
00:16:17,166 --> 00:16:20,253
We know you're Brian O'Conner,
formerly of the LAPD.

111
00:16:21,128 --> 00:16:23,214
- You got the wrong guy.
- Really?

112
00:16:28,177 --> 00:16:29,929
How you doing, O'Conner?

所以这是 curl 输出的一部分。在这种情况下,完整的输出达到 1000+(此处为 107-112)。现在:

$ [curl request] | grep -Po "^\d+$"
[empty response]

$ [curl request] | grep -Po "^\d+"
[shit ton of results]

我真的不明白。我也尝试添加-a到 grep ,但这也没有帮助。

为什么 $ 不起作用?还有一个更好的问题:为什么它会使每次点击都无效(也就是没有匹配项)?

编辑:上面 curl 片段的 xxd 输出

00000000: 3130 370d 0a30 303a 3135 3a35 342c 3933  107..00:15:54,93
00000010: 3620 2d2d 3e20 3030 3a31 353a 3536 2c36  6 --> 00:15:56,6
00000020: 3436 0d0a 5965 6168 2c20 7468 6973 2069  46..Yeah, this i
00000030: 7320 6869 6d2e 0d0a 5765 2067 6f74 2068  s him...We got h
00000040: 696d 2e0d 0a0d 0a31 3038 0d0a 3030 3a31  im.....108..00:1
00000050: 363a 3037 2c38 3233 202d 2d3e 2030 303a  6:07,823 --> 00:
00000060: 3136 3a31 312c 3836 390d 0a53 6f2c 2068  16:11,869..So, h
00000070: 6f77 206c 6f6e 670d 0a79 6f75 2062 6565  ow long..you bee
00000080: 6e20 696e 2053 6f75 7468 2046 6c6f 7269  n in South Flori
00000090: 6461 3f0d 0a0d 0a31 3039 0d0a 3030 3a31  da?....109..00:1
000000a0: 363a 3131 2c39 3533 202d 2d3e 2030 303a  6:11,953 --> 00:       
000000b0: 3136 3a31 332c 3837 310d 0a41 2077 6869  16:13,871..A whi
000000c0: 6c65 2e0d 0a42 6566 6f72 6520 7468 6174  le...Before that
000000d0: 3f0d 0a0d 0a31 3130 0d0a 3030 3a31 363a  ?....110..00:16:
000000e0: 3137 2c31 3636 202d 2d3e 2030 303a 3136  17,166 --> 00:16       
000000f0: 3a32 302c 3235 330d 0a57 6520 6b6e 6f77  :20,253..We know
00000100: 2079 6f75 2772 6520 4272 6961 6e20 4f27   you're Brian O'
00000110: 436f 6e6e 6572 2c0d 0a66 6f72 6d65 726c  Conner,..formerl
00000120: 7920 6f66 2074 6865 204c 4150 442e 0d0a  y of the LAPD...
00000130: 0d0a 3131 310d 0a30 303a 3136 3a32 312c  ..111..00:16:21,
00000140: 3132 3820 2d2d 3e20 3030 3a31 363a 3233  128 --> 00:16:23
00000150: 2c32 3134 0d0a 2d20 596f 7520 676f 7420  ,214..- You got
00000160: 7468 6520 7772 6f6e 6720 6775 792e 0d0a  the wrong guy...
00000170: 2d20 5265 616c 6c79 3f0d 0a0d 0a31 3132  - Really?....112
00000180: 0d0a 3030 3a31 363a 3238 2c31 3737 202d  ..00:16:28,177 -
00000190: 2d3e 2030 303a 3136 3a32 392c 3932 390d  -> 00:16:29,929.
000001a0: 0a48 6f77 2079 6f75 2064 6f69 6e67 2c20  .How you doing,
000001b0: 4f27 436f 6e6e 6572 3f0d 0a              O'Conner?..
command-line
  • 1 1 个回答
  • 875 Views

1 个回答

  • Voted
  1. Best Answer
    steeldriver
    2021-06-22T03:35:47+08:002021-06-22T03:35:47+08:00

    您的 curl 命令输出具有 DOS 样式的 CRLF 行结尾 - 因此您要查找的行不以 结尾\d+,它们以\d+\r

    您可以将 grep 命令更改为grep -Po "^\d+\r$"- 这将匹配您要查找的内容,但输出将包括回车符。使用彩色输出(即,当grep别名到grep --color=auto终端并输出到终端时)CR 会导致输出被颜色代码字符改写,使其显示为空。如果您正在管道或重定向输出,这可能不是问题。否则,一些选项是:

    • 通过管道输出 curltr以删除回车前。

       curl ... | tr -d '\r' | grep -Po "^\d+$"
      
    • 使用 Perl前瞻将 RE 更改为匹配但不包括 CR

       curl ... | grep -Po "^\d+(?=\r$)"
      
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve