linux - CURL 进度条 : How to pipe and extract numbers only using grep?

标签 linux shell command-line curl grep

这是我目前所拥有的:

[my1@graf home]$ curl -# -o f1.flv 'http://osr.com/f1.flv' |  grep -o '*[0-9]*'
####################################################################### 100.0%

我希望使用 grep 并仅从 CURL 输出的进度条中提取百分比。

我认为我的正则表达式不正确,我也不确定这个 grep 是否会影响持续更新的百分比?

我想做的基本上是让 CURL 只给我百分比数字作为输出,没有别的。

感谢您的帮助。

最佳答案

使用 curl 7.36.0(应该也适用于其他版本),您可以通过以下方式提取百分比:

curl ... 2>&1 -# | stdbuf -oL tr '\r' '\n' | grep -o '[0-9]*\.[0-9]'

此处 ... 代表选项/文件名。这将输出一系列百分比数字。

Curl 在其输出中使用回车符 \r,因此您需要先使用 tr 将它们转换为 \n,因为 grep 是面向行的。您还需要使用 stdbuf 修改输出缓冲区设置,以便在 curl 输出它们后立即获得百分比数字。

关于linux - CURL 进度条 : How to pipe and extract numbers only using grep?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132456/

相关文章:

swift - NSUserUnixTask 悄悄忽略脚本

linux - 将文件的输出一行发送到远程 Linux 机器

java - 通过 shell 传递空格分隔值需要比预期更多的参数

linux - 如何转义作为 shell 脚本中命令的关键字?

c - 在 linux 中测量 c 程序的运行时间

linux - 从 while 循环中导出变量

command-line - 如何以编程方式创建chrome crx文件(最好在Java中)?

python - 如何使用 Python 控制键盘和鼠标?

linux - 从 TestContainer 访问 Podman REST API

linux - 循环 Bash Shell 脚本