4

When I'm saying something like

curl http://example.com/123

and I want to read only n first lines, how do I do that? I know that's something like:

curl http://example.com/123 | ??? 
10

The command you're looking for is head -n D where D can be any integer number. Example:

curl http://example.com/123 | head -n 3
| improve this answer | |
  • how do read the last n lines? – Alan Coromano Oct 5 '13 at 5:17
  • Easy: tail -n – Sadi Oct 5 '13 at 9:29
  • It seems head -3 and head -n 3 and head --lines=3 are all the same. – Sadi Mar 6 '18 at 18:35
1

For curl in particular, there is an option to only load a range of bytes. Because it's bytes instead of lines, you may need to overestimate and then trim with head or tail, but this might save load-time on long webpages (or the byte-range may be sufficient for your application.) For example,

curl --range 0-99 http://example.com/123

will get the first 100 bytes. (You can also pull out a range in the middle of the page, give multiple disjoint ranges, or use a range measured from the end of the page.)

(I don't know for sure, but I think curl will load the whole webpage even if it's piped to a command that only reads the first few lines.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.