夏期劇*_*期劇場 7 curl stdout cut head
我试图只获取curl命令输出的第一行。(对不起,如果这令人困惑)
比方说,例如,我简单地运行:
# curl http://localhost
<!-- This is the hidden line i want to grab. -->
<!DOCTYPE html>
<html>
<head>
..
..
Run Code Online (Sandbox Code Playgroud)
如果我想要这里输出的第一行,该怎么办,即:
<!-- This is the hidden line i want to grab. -->
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这样的事情,但还没有运气:
# curl http://localhost | head -n 1
# curl http://localhost | sed -n '1!p'
Run Code Online (Sandbox Code Playgroud)
.. 等所有给我垃圾输出,像这样:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<!-- This is the hidden line i want to grab. -->
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (23) Failed writing body (173 != 1763)
Run Code Online (Sandbox Code Playgroud)
这不是上面提到的预期输出:
<!-- This is the hidden line i want to grab. -->
Run Code Online (Sandbox Code Playgroud)
有高手请看=(
这种所谓的垃圾输出基本上是下载数据操作期间的进度表。你基本上可以忽略它,因为它默认进入被忽略的标准错误流,所以只有相关部分被打印到标准输出。
这是测试:
$ curl http://example.com/ | head -n1 > example.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1270 100 1270 0 0 112k 0 --:--:-- --:--:-- --:--:-- 124k
(23) Failed writing body
$ cat example.html
<!doctype html>
Run Code Online (Sandbox Code Playgroud)
如果您仍想使其静音,请-s为安静模式添加参数或将标准错误流重定向到/dev/null,例如:
$ curl -s http://example.com/ 2> /dev/null | head -n1
<!doctype html>
Run Code Online (Sandbox Code Playgroud)
或者使用命令替换:
head -n1 <(curl -s http://example.com/ 2> /dev/null)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9752 次 |
| 最近记录: |