以下非常简洁的命令将打印模式第一次出现(包括第一次出现)后的所有行:
awk '/pattern/,0'
有人可以解释这个命令是如何工作的吗? awk 如何解析“/pattern/,0”?
(顺便说一下,这个不是我想出来的;它发布在 compgroups.net 上。)
最佳答案
每 awk
手册页:
Patterns are arbitrary Boolean combinations (with ! || &&) of regular expressions and relational expressions. ...
A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines from an occurrence of the first pattern though an occurrence of the second. ...
这里第一个是
/pattern/
第二个是文字常量 0
,这是错误的。所以这从匹配的第一行开始,当一行根本不存在时停止,这只会在文件结束后发生。再举一个例子,比较:
jot 10
和:
jot 10 | awk 'NR==4,NR==6 { printf("line %d: %s\n", NR, $0) }'
关于awk - 这个 awk 命令是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18262022/