打印文件中从匹配行到文件末尾的行数

问题描述 投票:31回答:2

我写了下面的awk来打印从匹配行到EOF的行数。

awk '/match_line/,/*/' file

如何在 sed 中做同样的事情?

sed
2个回答
58
投票
sed -n '/matched/,$p' file
awk '/matched/,0' file

17
投票

这是在Windows上使用的GNU sed的老版本。

GNU sed 2.05版

http:/www.gnu.orgsoftwaresedmanualsed.html

-n only display if Printed
-e expression to evaluate
p stands for Print
$ end of file
line1,line2 is the range
! is NOT

干草堆.txt

abc
def
ghi
needle
want 1
want 2

将匹配的行和下面的行打印到文件末尾。

>sed.exe -n -e "/needle/,$p" haystack.txt
needle
want 1
want 2

打印文件的起始行,直到但不包括匹配行。

>sed.exe -n -e "/needle/,$!p" haystack.txt
abc
def
ghi

打印文件的开始部分,包括匹配的行。

>sed.exe -n -e "1,/needle/p" haystack.txt
abc
def
ghi
needle

打印匹配行后的所有内容

>sed.exe -n -e "1,/needle/!p" haystack.txt
want 1
want 2

打印两场比赛之间的所有内容--包括

>sed.exe -n -e "/def/,/want 1/p" haystack.txt
def
ghi
needle
want 1
© www.soinside.com 2019 - 2024. All rights reserved.