我正在尝试使用 wget
下载文件,并希望将进度存储在日志文件中。
wget http://unfccc.int/resource/docs/convkp/kpeng.pdf -O amit.pdf 2> amit.log
这是 wget 返回的内容:
Connecting to unfccc.int (unfccc.int)|62.225.2.55|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 76721 (75K) [application/pdf]
Saving to: `amit.pdf'
0K .......... .......... .......... .......... .......... 66% 2.29M 0s
50K .......... .......... .... 100% 9.41M=0.02s
2013-08-28 12:24:32 (3.06 MB/s) - `amit.pdf' saved [76721/76721]
我想使用 grep 获取最后一行的百分比值。这是我与上面的命令一起使用的,但它不起作用:
| grep "(\d+(\.\d+)?(?=%)" amit.log
是否可以仅使用 grep 而不使用 sed 和 awk?
最佳答案
- 您的模式包含不匹配的
(
。 - 您需要告诉
grep
您正在使用 PCRE。 - 由于您将
STDERR
重定向到文件,因此使用管道进行grep
没有多大意义。相反,将STDERR
重定向到STDOUT
,然后通过管道传输输出。
尝试:
grep -oP "(\d+(\.\d+)?(?=%))"
如果你想使用管道,请说:
wget http://unfccc.int/resource/docs/convkp/kpeng.pdf -O amit.pdf 2>&1 | grep -oP "(\d+(\.\d+)?(?=%))"
编辑:如果 stderr
需要记录到文件中,请说:
wget http://unfccc.int/resource/docs/convkp/kpeng.pdf -O amit.pdf 2>&1 | tee amit.log | grep -oP "(\d+(\.\d+)?(?=%))"
关于bash - 如何使用 grep 从最后一行提取百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482841/