Say I have a huge text file (>2GB) and I just want to cat
the lines X
to Y
(e.g. 57890000 to 57890010).
From what I understand I can do this by piping head
into tail
or viceversa, i.e.
head -A /path/to/file | tail -B
or alternatively
tail -C /path/to/file | head -D
where A
,B
,C
and D
can be computed from the number of lines in the file, X
and Y
.
But there are two problems with this approach:
- You have to compute
A
,B
,C
andD
. - The commands could
pipe
to each other many more lines than I am interested in reading (e.g. if I am reading just a few lines in the middle of a huge file)
Is there a way to have the shell just work with and output the lines I want? (while providing only X
and Y
)?
Best Answer
I suggest the
sed
solution, but for the sake of completeness,To cut out after the last line:
Speed test (here on macOS, YMMV on other systems):
seq 100000000 > test.in
real
time as reported bybash
's builtintime
These are by no means precise benchmarks, but the difference is clear and repeatable enough* to give a good sense of the relative speed of each of these commands.
*: Except between the first two,
sed -n p;q
andhead|tail
, which seem to be essentially the same.