AWK常用技巧
1.1 介绍
1.1.1 AWK原理
似于C 语言。在代码块中只有一条 print 命令。在awk 中,如果只出现 print 命令,那么将打印当前行的全部内容。
1.2 AWK常用速查表
1.2.1 AWK运算符
<td style="width: 63.44%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt; height: 1pt;" valign="top" width="63%">
<p style="text-align: center;" align="center">
<strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">说明</span></strong>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">赋值语句</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">逻辑或</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">逻辑与</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">匹配正则表达式和不匹配正则表达式</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">关系运算符</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">加,减</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">乘,除与求余</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">一元加,减和逻辑非</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">求幂</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">增加或减少,作为前缀或后缀</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">字段引用</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">字符串链接符</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 3.15pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">三目运算符</span>
</p>
</td>
<td style="width: 63.44%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" valign="top" width="63%">
<p>
<span style="font-family: '微软雅黑',sans-serif;">数组中是否存在某键值</span>
</p>
</td>
1.2.2 常用AWK内置变量
<td style="width: 83.32%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: center;" align="center">
<strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">属</span></strong><strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new"; color: white;">性</span></strong>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">当前记录</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">当前记录的第</span> n <span style="font-family: '微软雅黑',sans-serif;">个字段</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">输入字段分隔符</span> <span style="font-family: '微软雅黑',sans-serif;">默认是空格</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">输入记录分割符</span> <span style="font-family: '微软雅黑',sans-serif;">默认为换行符</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">当前记录中的字段个数,就是有多少列</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">已经读出的记录数,就是行号,从</span> 1 <span style="font-family: '微软雅黑',sans-serif;">开始</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">输出字段分隔符</span> <span style="font-family: '微软雅黑',sans-serif;">默认也是空格</span>
</p>
</td>
<td style="width: 83.32%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 22.6pt;" width="83%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">输出的记录分隔符</span> <span style="font-family: '微软雅黑',sans-serif;">默认为换行符</span>
</p>
</td>
1.2.3 awk中的正则
<td style="width: 27.58%; border-top: solid #9BBB59 1.0pt; border-left: none; border-bottom: solid #9BBB59 1.0pt; border-right: none; background: #9BBB59; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">功能</span></strong>
</p>
</td>
<td style="width: 20.34%; border-top: solid #9BBB59 1.0pt; border-left: none; border-bottom: solid #9BBB59 1.0pt; border-right: none; background: #9BBB59; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">示例</span></strong>
</p>
</td>
<td style="width: 41.74%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt; height: 1pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">解释</span></strong>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">行首定位符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/^root/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配所有以</span> root <span style="font-family: '微软雅黑',sans-serif;">开头的行</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">行尾定位符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/root$/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配所有以</span> root <span style="font-family: '微软雅黑',sans-serif;">结尾的行</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配任意单个字符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/r..t/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配字母</span> r,<span style="font-family: '微软雅黑',sans-serif;">然后两个任意字符,再以</span> l
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">结尾的行,比如</span> root,r33l <span style="font-family: '微软雅黑',sans-serif;">等</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> 0 <span style="font-family: '微软雅黑',sans-serif;">个或多个前导字符</span>(<span style="font-family: '微软雅黑',sans-serif;">包括回车</span>)
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/a*ool/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> 0 <span style="font-family: '微软雅黑',sans-serif;">个或多个</span> a <span style="font-family: '微软雅黑',sans-serif;">之后紧跟着</span> ool <span style="font-family: '微软雅黑',sans-serif;">的行,比如</span> ool<span style="font-family: '微软雅黑',sans-serif;">,</span>aaaaool <span style="font-family: '微软雅黑',sans-serif;">等</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> 1 <span style="font-family: '微软雅黑',sans-serif;">个或多个前导字符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/a+b/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> 1 <span style="font-family: '微软雅黑',sans-serif;">个或多个</span> a <span style="font-family: '微软雅黑',sans-serif;">加</span> b <span style="font-family: '微软雅黑',sans-serif;">的行,比如</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
ab,aab <span style="font-family: '微软雅黑',sans-serif;">等</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> 0 <span style="font-family: '微软雅黑',sans-serif;">个或</span> 1 <span style="font-family: '微软雅黑',sans-serif;">个前导字符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/a?b/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> b <span style="font-family: '微软雅黑',sans-serif;">或</span> ab <span style="font-family: '微软雅黑',sans-serif;">的行</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配指定字符组内的任意一个字符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/^[abc]
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配以字母</span> a <span style="font-family: '微软雅黑',sans-serif;">或</span>b <span style="font-family: '微软雅黑',sans-serif;">或</span> c <span style="font-family: '微软雅黑',sans-serif;">开头的行</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配不在指定字符组内任意一个字符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/^[^abc]/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配不以字母</span> a <span style="font-family: '微软雅黑',sans-serif;">或</span> b <span style="font-family: '微软雅黑',sans-serif;">或</span> c <span style="font-family: '微软雅黑',sans-serif;">开头的行</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">子表达式组合</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/(rool)+/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">表示一个或多个</span> rool <span style="font-family: '微软雅黑',sans-serif;">组合,当有一些字符需要组合时,使用括号括起来</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">或者的意思</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/(root)|B/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span>root <span style="font-family: '微软雅黑',sans-serif;">或者</span> B <span style="font-family: '微软雅黑',sans-serif;">的行</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">转义字符</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/a\/\//
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配</span> a//
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配,不匹配的条件语句</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
$1~/root/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">匹配第一个字段包含字符</span>root <span style="font-family: '微软雅黑',sans-serif;">的所有记录</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
x <span style="font-family: '微软雅黑',sans-serif;">重复</span>m <span style="font-family: '微软雅黑',sans-serif;">次</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/(root){3}/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">需要注意一点的是,</span>root <span style="font-family: '微软雅黑',sans-serif;">加括号和不</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
x <span style="font-family: '微软雅黑',sans-serif;">重复至少</span>m <span style="font-family: '微软雅黑',sans-serif;">次</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/(root){3,}/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">加括号的区别,</span>x <span style="font-family: '微软雅黑',sans-serif;">可以表示字符串也</span>
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
x <span style="font-family: '微软雅黑',sans-serif;">重复至少</span> m <span style="font-family: '微软雅黑',sans-serif;">次,</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">但不超过</span> n <span style="font-family: '微软雅黑',sans-serif;">次</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/(root){5,6}/
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">可以只是一个字符,所以</span>/root\{5\}/
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">表示匹配</span>roo <span style="font-family: '微软雅黑',sans-serif;">再加上</span>5 <span style="font-family: '微软雅黑',sans-serif;">个</span>t<span style="font-family: '微软雅黑',sans-serif;">,及</span>roottttt
</p>
</td>
<td style="width: 27.58%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="27%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">需要指定参数:</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
-posix <span style="font-family: '微软雅黑',sans-serif;">或者</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
--re-interval <span style="font-family: '微软雅黑',sans-serif;">没</span> <span style="font-family: '微软雅黑',sans-serif;">有该参数不能使用该模式</span>
</p>
</td>
<td style="width: 20.34%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="20%">
<p style="text-align: justify; text-justify: inter-ideograph;">
cat rex.txt smierth,harry smierth,reru robin,tom
</p>
</td>
<td style="width: 41.74%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="41%">
<p style="text-align: justify; text-justify: inter-ideograph;">
/\(root\)\{2,\}/ <span style="font-family: '微软雅黑',sans-serif;">则</span> <span style="font-family: '微软雅黑',sans-serif;">表</span> <span style="font-family: '微软雅黑',sans-serif;">示</span> <span style="font-family: '微软雅黑',sans-serif;">匹</span> <span style="font-family: '微软雅黑',sans-serif;">配</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
rootrootrootroot <span style="font-family: '微软雅黑',sans-serif;">等</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
awk -posix '/er\{1,2\}/' rex.text smierth,harry
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
smierth,reru
</p>
</td>
1.2.4 awk 常用函数表
<td style="width: 74.68%; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt; height: 1pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">说明</span></strong>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">除了正则表达式所有具体值被替代这点,它和</span> sub <span style="font-family: '微软雅黑',sans-serif;">函数完全一样地执行,。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">用</span> Repl <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串替换</span> In <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串中的由</span> Ere<span style="font-family: '微软雅黑',sans-serif;">参数指定的扩展正则表达式的第一个具体值。</span>sub <span style="font-family: '微软雅黑',sans-serif;">函数返回替换的数量。出现在</span> Repl <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串中的</span> &<span style="font-family: '微软雅黑',sans-serif;">(和符号)由</span> In <span style="font-family: '微软雅黑',sans-serif;">参数指定的与</span> Ere <span style="font-family: '微软雅黑',sans-serif;">参数的指定的扩展正则表达式匹配的字符串替换。如果未指定</span> In <span style="font-family: '微软雅黑',sans-serif;">参数,缺省值是整个记录(</span>$0 <span style="font-family: '微软雅黑',sans-serif;">记录变量)。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">在由</span> String1 <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串(其中有出现</span> String2 <span style="font-family: '微软雅黑',sans-serif;">指定的参数)中,返回位置,从</span> 1 <span style="font-family: '微软雅黑',sans-serif;">开始编号。如果</span> String2 <span style="font-family: '微软雅黑',sans-serif;">参数不在</span> String1 <span style="font-family: '微软雅黑',sans-serif;">参数中出现,则返回</span> 0<span style="font-family: '微软雅黑',sans-serif;">(零)。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">返回</span> String <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串的长度(字符形式)。如果未给出</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
String <span style="font-family: '微软雅黑',sans-serif;">参数,则返回整个记录的长度(</span>$0 <span style="font-family: '微软雅黑',sans-serif;">记录变量)。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">返回</span> String <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串的长度(以字节为单位)。如果未给出</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
String <span style="font-family: '微软雅黑',sans-serif;">参数,则返回整个记录的长度(</span>$0 <span style="font-family: '微软雅黑',sans-serif;">记录变量)。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">返回具有</span> N <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符数量子串。子串从</span> String <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串取得,其字符以</span> M <span style="font-family: '微软雅黑',sans-serif;">参数指定的位置开始。</span>M <span style="font-family: '微软雅黑',sans-serif;">参数指定为将</span>
</p>
<p style="text-align: justify; text-justify: inter-ideograph;">
String <span style="font-family: '微软雅黑',sans-serif;">参数中的第一个字符作为编号</span> 1<span style="font-family: '微软雅黑',sans-serif;">。如果未指定</span> N <span style="font-family: '微软雅黑',sans-serif;">参数,则子串的长度将是</span> M <span style="font-family: '微软雅黑',sans-serif;">参数指定的位置到</span> String <span style="font-family: '微软雅黑',sans-serif;">参数的末尾</span> <span style="font-family: '微软雅黑',sans-serif;">的长度。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt; height: 75.05pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">在</span> String <span style="font-family: '微软雅黑',sans-serif;">参数指定的字符串(</span>Ere <span style="font-family: '微软雅黑',sans-serif;">参数指定的扩展正则表达式出现在其中)中返回位置(字符形式),从</span> 1 <span style="font-family: '微软雅黑',sans-serif;">开始编号,或如果</span> Ere <span style="font-family: '微软雅黑',sans-serif;">参数不出现,则返回</span> 0<span style="font-family: '微软雅黑',sans-serif;">(零)。</span>RSTART <span style="font-family: '微软雅黑',sans-serif;">特殊变量设置为返回值。</span>RLENGTH<span style="font-family: '微软雅黑',sans-serif;">特殊变量设置为匹配的字符串的长度,或如果未找到任何匹配,则设置为</span> -1<span style="font-family: '微软雅黑',sans-serif;">(负一)。</span>
</p>
</td>
<td style="width: 74.68%; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; height: 1.0pt;" width="74%">
<p style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-family: '微软雅黑',sans-serif;">将</span> String <span style="font-family: '微软雅黑',sans-serif;">参数指定的参数分割为数组元素</span> A[1], A[2], . . ., A[n]<span style="font-family: '微软雅黑',sans-serif;">,并返回</span> n <span style="font-family: '微软雅黑',sans-serif;">变量的值。此分隔可以通过</span> Ere <span style="font-family: '微软雅黑',sans-serif;">参数指定的扩展正则表达式进行,或用当前字段分隔符(</span>FS <span style="font-family: '微软雅黑',sans-serif;">特殊变量)来进行(如果没有给出</span> Ere<span style="font-family: '微软雅黑',sans-serif;">参数)。除非上下文指明特定的元素还应具有一个数字值,否则</span> A <span style="font-family: '微软雅黑',sans-serif;">数</span>
</p>
</td>
1.3 AWK实践
1.3.1 函数的简单使用
1.3.2 统计系统中的secure文件中谁在破解你的密码
1、找到谁在破解密码
2、过滤出包含Failed password的行 并统计每个ip地址出现的次数
1.3.3 统计secure文件中每个用户,被同一ip破解多少次
1、进破解使用的用户及地址定向到文件中
2、对文件进行去重排序测
<p>
[2] <a href="https://www.cnblogs.com/ginvip/p/6352157.html">https://www.cnblogs.com/ginvip/p/6352157.html</a>
</p>
<li>
<a href="#12_AWK">1.2 AWK常用速查表</a><ul>
<li>
<a href="#121_AWK">1.2.1 AWK运算符</a>
</li>
<li>
<a href="#122_AWK">1.2.2 常用AWK内置变量</a>
</li>
<li>
<a href="#123_awk">1.2.3 awk中的正则</a>
</li>
<li>
<a href="#124_awk">1.2.4 awk 常用函数表</a>
</li>
</ul>
</li>
<li>
<a href="#13_AWK">1.3 AWK实践</a><ul>
<li>
<a href="#131">1.3.1 函数的简单使用</a>
</li>
<li>
<a href="#132_secure">1.3.2 统计系统中的secure文件中谁在破解你的密码</a>
</li>
<li>
<a href="#133_secureip">1.3.3 统计secure文件中每个用户,被同一ip破解多少次</a>
</li>
<li>
<a href="#134_nginx_accesslogip">1.3.4 统计nginx access.log文件中对ip地址去重并统计重复数</a>
</li>
<li>
<a href="#135_accesslogip">1.3.5 统计access.log文件中每个ip地址使用了多少流量</a>
</li>
<li>
<a href="#136_accesslogipip">1.3.6 统计access.log文件中每个ip地址使用了多少流量和每个ip地址的出现次数</a>
</li>
<li>
<a href="#137">1.3.7 按要求得到最后面的格式的结果</a>
</li>
<li>
<a href="#138">1.3.8 统计每个学生的总成绩和平均成绩</a>
</li>
<li>
<a href="#139_6">1.3.9 打印下面语句中字符数小于6的单词</a>
</li>
</ul>
</li>
<li>
<a href="#14">1.4 附录</a><ul>
<li>
<a href="#141_sort">1.4.1 sort命令的使用</a>
</li>
<li>
<a href="#142">1.4.2 统计流量使用的命令</a>
</li>
<li>
<a href="#143">1.4.3 其他日志分析方法</a>
</li>
</ul>
</li>
<li>
<a href="#15">1.5 参考文献</a>
</li>
</ul>
- 原文作者:惨绿少年
- 原文链接:https://clsn.io/clsn/lx130.html
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。