简体   繁体   English

Bash从Z行开始从文件读取N行

[英]Bash read N lines from file starting from line Z

I have text file: 我有文本文件:

34
3467uy
56ytyyu
And so on

I want to read from this file N lines starting from line Z. I know how to read first N lines with (head -n N) or last with tail. 我想从线Z开始这个文件的N行我知道如何阅读第一N行与(头-N N)或最后用尾巴阅读。 I know now how to read from line Z to the end of the file with tail. 我现在知道如何从Z行读取到文件尾的尾部。 But I can't find how to read specific amount starting from specific line. 但我找不到如何读取特定量的特定行开始。 Help! 救命!

This can be accomplished using sed . 这可以使用sed完成。 For example, to emit lines 10 through 20 in a file: 例如,要在文件中发出第10至20行:

sed -n '10,+10p' a_file

Pipe tail 's output to head : tail的输出head

tail -n +Z file | head -n N

For example: 例如:

$ seq 100 | tail -n +15 | head -n 5
15
16
17
18
19

使用awk :下面的示例将从第3行打印到第5行(3 + 2行)。设置start_linebuffer变量以从start_line开始打印该行并buffer更多行。

awk -vstart_line=3 -vbuffer=2 'NR>=start_line && NR<=start_line+buffer' file

With GNU sed: 使用GNU sed:

seq 1 10 | sed '2,+3!d'

Output: 输出:

2
3
4
5

From man sed : 来自man sed

addr1,+N : Will match addr1 and the N lines following addr1. addr1,+N :将匹配addr1和addr1之后的N行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM