Read only n first lines from something

5,711

Solution 1

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

Solution 2

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

Share:
5,711

Related videos on Youtube

seddka
Author by

seddka

Updated on September 18, 2022

Comments

  • seddka
    seddka over 1 year

    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 | ??? 
    
  • seddka
    seddka over 10 years
    how do read the last n lines?
  • Sadi
    Sadi over 10 years
    Easy: tail -n
  • Sadi
    Sadi about 6 years
    It seems head -3 and head -n 3 and head --lines=3 are all the same.