bash 在shell中提取两个行号之间的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21759562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:33:17  来源:igfitidea点击:

Extract lines between two line numbers in shell

bashshellsed

提问by HobbitOfShire

How is this possible in shell using sedor any other filter

这怎么可能在 shell 中使用sed或任何其他过滤器

Lets say that i have two specific line numbers in two variables $line1and $line2and i want to extract lines between these two lines like this

假设我在两个变量中有两个特定的行号$line1$line2并且我想像这样提取这两行之间的行

cat my_file_path | command $line1 $line2

Example if my file is:

例如,如果我的文件是:

1:bbb

1:bb

2:cccc

2:cccc

3:dddd

3:dddd

4:eeeee

4:eeee

My output should be if i do:

如果我这样做,我的输出应该是:

cat my_file_path | command 1 3

Output

输出

bbb
cccc

采纳答案by Kent

without testing:

未经测试:

awk -v s="$line1" -v e="$line2" 'NR>s&&NR<e' file

you can control the "inclusive/exclusive" with >= or <=

你可以控制“包含/独占” >= or <=

if you want to make it safe for leading/trailing spaces in your shell var:

如果您想让 shell var 中的前导/尾随空格安全:

 awk -v s="$line1" -v e="$line2" 'NR>1*s&&NR<1*e' file

回答by Mihai

Using sed:

使用 sed:

$ cat my_file_path | sed -n "${line1},${line2}p"

or, even better (catis somehow redundant):

或者,甚至更好(cat在某种程度上是多余的):

$ sed -n "${line1},${line2}p" my_file_path

回答by user1699917

You can use a pipe with tail and head

您可以使用带有尾部和头部的管道

tail -n +$line1 | head -n $((line2-line1))

回答by Enrique Palacio

This is what I use on Centos, this example print lines between 100 and 200 from myFile.log

这是我在 Centos 上使用的,此示例从myFile.log打印 100 到 200 之间的行

 awk 'NR >= 100 && NR <= 200' myFile.log

Other example lines before 100

100 之前的其他示例行

awk  'NR <= 100' fileName.log

Note that You can also use >,< comparators.

请注意,您还可以使用 >,< 比较器。

This answer use NR variable aka record number, like line number, for more complex scenarios You can take a look to AWK build in variables: https://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

对于更复杂的场景,此答案使用 NR 变量又名记录号,例如行号您可以查看 AWK 内置变量:https: //www.thegeekstuff.com/2010/01/8-powerful-awk-built- in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

hope It helps

希望能帮助到你

回答by Robin A. Meade

Using awk:

使用 awk:

awk 'NR==100, NR==200' myfile.log
awk 'NR==100, NR==200; NR==200 {exit}' myfile.log # optimized

The 2nd has the optimization that it exits after the last line of the desired range has been printed.

第二个优化是在打印完所需范围的最后一行后退出。

A range patternwas used:

使用了范围模式

A range pattern is made of two patterns separated by a comma, in the form ‘begpat, endpat'. It is used to match ranges of consecutive input records.
https://www.gnu.org/software/gawk/manual/html_node/Ranges.html

范围模式由两个由逗号分隔的模式组成,格式为“begpat, endpat”。它用于匹配连续输入记录的范围。
https://www.gnu.org/software/gawk/manual/html_node/Ranges.html

A patterncan be either a regexp patternor an expression pattern. Above uses expression patternsto do comparisons with NR.

图案可以是正规表达式表达模式。以上使用表达模式与 NR 进行比较。