regex - 使用awk打印从匹配到文件结尾的行

标签 regex awk

Print lines in file from the match line until end of file我了解到您可以使用 0awk 中的范围表达式中匹配到文件末尾:

awk '/matched/,0' file

然而,今天我开始玩这个0我发现这也有效:
awk '/matched/,/!./' file

我猜 0正在工作,因为在范围表达式 /regex1/,/regex2/ 中打印行直到 regex2评估为 True。从 0评估为 False,这永远不会发生,它最终会打印所有行。这是正确的吗?

但是,与 /!./我想这会一直工作,直到找到没有任何字符的行,就像我们说 awk '/matched/,!NF' .但它没有:相反,它总是评估为 False。这是什么原因?

例子
$ cat a
1
matched
2

3
4
end

两者 awk '/matched/,/!./' aawk '/matched/,0' a返回:
matched
2

3
4
end

awk '/matched/,!NF' a返回:

匹配
2

最佳答案

你在错误的地方感叹。像这样将其保留在正则表达式之外 取消匹配 :

awk '/matched/,!/./' a
matched
2


Your regex /!./ is matching a literal ! followed by any character.

If your file is this:

cat a
1
matched
2

3
!4
end

然后
awk '/matched/,/!./' a
matched
2

3
!4

和:
awk '/matched/,!/./' a

匹配
2

关于regex - 使用awk打印从匹配到文件结尾的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28742712/

相关文章:

regex - 如何在 Perl 中使用正则表达式检查字符串是否符合精确模式

awk - 如何在awk中简单地将这个字符串解析为kv

linux - 比较 2 个文件并仅删除重复行一次

regex - 如何在 Google BigQuery 中的 URL 字符串中的模式后提取带有 SYMBOLS 的字符串

linux - nslookup和awk获得第二个地址

awk - 使用 awk,删除不同索引中具有重复列对的行

bash - 协助 awk/bash 捕获内存差异

java - 在java中使用正则表达式将文本文件分成 block

regex - 使用命令行,我如何得到每种类型的 n 个可能结果?

Python 正则表达式否定回顾