How to print lines between 2 values using tail & head and pipe?

639 views Asked by At

For example:how can I print specific lines of a .txt file between line 5 and line 8 using only tail and head

2

There are 2 answers

0
Kevin C On BEST ANSWER

Copied from here

infile.txt contains a numerical value on each line.

➜   X=3
➜   Y=10
➜   < infile.txt tail -n +"$X" | head -n "$((Y - X))"
3
4
5
6
7
8
9
➜  
0
rakesh On

if a.txt contains lines below:

products, apple, banana, carrot, dairy,

and if you want to print lines between 1 and 5 i.e-apple,banana,carrot.you can use below command:

cat a.txt | head -n -5 | tail +2