awk - 这个 awk 命令是如何工作的?

标签 awk

以下非常简洁的命令将打印模式第一次出现(包括第一次出现)后的所有行:

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/

相关文章:

linux - 在所需列中查找重复项并在 awk 中打印所选模式?

python - 从 HTML 中选取图像 URL

linux - 我需要将文件中指定的模式列表与另一个文件进行比较,并仅报告包含模式的列的匹配部分

linux - 从每个目录复制 png,如果 png 大于 600x600?

bash - gnu并行中的两对双引号冲突

linux - Linux 中两个文件重叠或分别用 file2 中的行替换 file1 中的行

linux - 使用 sed、awk、grep 转换文本

linux - Bash 脚本记录平均 ping 时间,一天每 20 秒一次

bash - sed/awk - 如何删除少于 3 列的行

linux - 如何使用sed或awk处理指定列的内容?