M HYPE SPLASH
// general

Read only n first lines from something

By John Peck

When I'm saying something like

curl 

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

curl | ??? 

2 Answers

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

curl | head -n 3
3

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 

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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