Is there a way in Linux to ask for the Head or Tail but with an additional offset of records to ignore.
For example if the file example.lst
contains the following:
row01
row02
row03
row04
row05
And I use head -n3 example.lst
I can get rows 1 - 3 but what if I want it to skip the first row and get rows 2 - 4?
I ask because some commands have a header which may not be desirable within the search results. For example du -h ~ --max-depth 1 | sort -rh
will return the directory size of all folders within the home directory sorted in descending order but will append the current directory to the top of the result set (i.e. ~
).
The Head and Tail man pages don't seem to have any offset parameter so maybe there is some kind of range
command where the required lines can be specified: e.g. range 2-10
or something?
To get the rows between 2 and 4 (both inclusive), you can use:
or
It took make a lot of time to end-up with this solution which, seems to be the only one that covered all usecases (so far):
Feature list:
From
man tail
:You can therefore use
... | tail -n +2 | head -n 3
to get 3 lines starting from line 2.Non-head/tail methods include
sed -n "2,4p"
andawk "NR >= 2 && NR <= 4"
.