Read only n first lines from something
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.)
Related videos on Youtube
seddka
Updated on September 18, 2022Comments
-
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 over 10 yearshow do read the last n lines?
-
Sadi over 10 yearsEasy: tail -n
-
Sadi about 6 yearsIt seems head -3 and head -n 3 and head --lines=3 are all the same.