Shell:获取文件的第x行到第y行

nav*_*ige 8 unix shell

我可以使用什么unix shell命令从文件中获取行x(例如10)到y(例如到15).grep似乎没有帮助,除了做一个for循环,我想不出别的什么.

P.P*_*.P. 22

你可以使用sed:

sed -n '5,10p' filename
Run Code Online (Sandbox Code Playgroud)

打印5到10行.


far*_*ncz 7

头y,尾yx

head -n 15 filename | tail -n 5
Run Code Online (Sandbox Code Playgroud)


Vij*_*jay 5

awk:

awk 'NR>=10 and NR<=15' your_file
Run Code Online (Sandbox Code Playgroud)

珀尔:

perl -lne 'print if($.>=10 && $.<=15)' your_file
Run Code Online (Sandbox Code Playgroud)

测试如下:

> 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
>
Run Code Online (Sandbox Code Playgroud)