8

I'm trying to get only first line of the curl command output. (Sorry if this is confusing)

Let's say, for an instance, i run simply:

# curl http://localhost
<!-- This is the hidden line i want to grab. -->
<!DOCTYPE html>
<html>
<head>
..
..

What to do if i want the very first line of the output here, which is:

<!-- This is the hidden line i want to grab. -->

I've tried things like this, but no luck yet:

# curl http://localhost | head -n 1
# curl http://localhost | sed -n '1!p'

.. etc. All gives me rubbish outputs, like this:

  % 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)

It's not the output as expected as mentioned above:

<!-- This is the hidden line i want to grab. -->

Any experts here please =(

3
  • What kind of rubbish output? This: curl -s http://example.com/ | head -n1 should work correctly, unless your output is compressed.
    – kenorb
    Feb 19, 2016 at 11:26
  • Edited the question to let you know what are the rubbish outputs means. Thanks. Feb 19, 2016 at 11:30
  • 1
    Just add -s parameter to mute any progress output (rubbish). This still goes to stderr, so it won't be saved into the file if you redirect it somewhere.
    – kenorb
    Feb 19, 2016 at 11:41

3 Answers 3

11

This so called rubbish output is basically a progress meter during operation of downloading the data. You can basically ignore that, since it is by default going into standard error stream which is ignored, so only relevant part is printed out to standard output.

Here is the test:

$ 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>

If you still want to silence it, add -s parameter for quiet mode or redirect standard error stream into /dev/null, for example:

$ curl -s http://example.com/ 2> /dev/null | head -n1
<!doctype html>

Or using command substitution:

head -n1 <(curl -s http://example.com/ 2> /dev/null)
0

You can also use AWK to print the first line of the output
Example:

$ curl -skI -XGET 'http://example.com' | awk 'NR==1'
HTTP/1.1 200 OK
0

I'll answer even if this is an old topic: instead of cutting the curl output, you may try with

$ curl http://localhost -o localhost.txt

$ head -n 1 localhost.txt

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 9, 2023 at 13:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .