navige
navige

Reputation: 2517

Shell: get lines x to y of file

What unix shell command can I use to get lines x (e.g. 10) to y (e.g. to 15) from a file. grep doesn't seem to help and except doing a for loop I can't think of anything else.

Upvotes: 17

Views: 19079

Answers (5)

Sidharth C. Nadhan
Sidharth C. Nadhan

Reputation: 2253

For bigger files :

sed '10,15! d;15q' file

Upvotes: 5

Vijay
Vijay

Reputation: 67301

awk:

awk 'NR>=10 and NR<=15' your_file

perl:

perl -lne 'print if($.>=10 && $.<=15)' your_file

tested below:

> cat temp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> nawk 'NR>=10&&NR<=15' temp
10
11
12
13
14
15
> perl -lne 'print if($.>=10&&$.<=15)' temp
10
11
12
13
14
15
>

Upvotes: 5

farincz
farincz

Reputation: 5173

head y, tail y-x

head -n 15 filename | tail -n 5

Upvotes: 11

pfnuesel
pfnuesel

Reputation: 15350

You can use head and tail, e.g.

head -n 15 $file | tail -n 5

Upvotes: 3

P.P
P.P

Reputation: 121407

You can use sed:

sed -n '5,10p' filename

to print lines from 5 to 10.

Upvotes: 39

Related Questions