awk内置变量

在awk中变量无须定义即可使用,变量在赋值时即已经完成了定义。变量的类型可以是数字、字符串。根据使用的不同,未初始化变量的值为0或空白字符串" ",这主要取决于变量应用的上下文。下面为变量的赋值负号列表:
符号含义等价形式
=a = 5a = 5
+=a = a + 5a += 5
-=a = a - 5a -= 5
*=a = a * 5a *= 5
/=a = a / 5a /= 5
%=a = a % 5a %= 5
^=a = a ^ 5a ^= 5
    [root@xieqichao ~]# awk '$1 ~ /Tom/ {Wage = $2 * $3; print Wage}' filename

 
 
  • 1
该命令将从文件中读取,并查找第一个域字段匹配Tom的记录,再将其第二和第三个字段的乘积赋值给自定义的Wage变量,最后通过print命令将该变量打印输出。

 
 
  • 1
    [root@xieqichao ~]# awk ' {$5 = 1000 * $3 / $2; print}' filename

 
 
  • 1

在上面的命令中,如果$5不存在,awk将计算表达式1000 * $3 / $2的值,并将其赋值给$5。如果第五个域存在,则用表达式覆盖$5原来的值。

我们同样也可以在命令行中定义自定义的变量,用法如下:

 [root@xieqichao ~]# awk -F: -f awkscript month=4 year=2011 filename

 
 
  • 1

这里的month和year都是自定义变量,且分别被赋值为4和2000,在awk的脚本中这些变量将可以被直接使用,他们和脚本中定义的变量在使用上没有任何区别。

除此之外,awk还提供了一组内建变量(变量名全部大写),见如下列表:
变量名变量内容
ARGC命令行参数的数量。
ARGIND命令行正在处理的当前文件的AGV的索引。
ARGV命令行参数数组。
CONVFMT转换数字格式。
ENVIRON从shell中传递来的包含当前环境变量的数组。
ERRNO当使用close函数或者通过getline函数读取的时候,发生的重新定向错误的描述信息就保存在这个变量中。
FIELDWIDTHS在对记录进行固定域宽的分割时,可以替代FS的分隔符的列表。
FILENAME当前的输入文件名。
FNR当前文件的记录号。
FS输入分隔符,默认是空格。
IGNORECASE在正则表达式和字符串操作中关闭大小写敏感。
NF当前文件域的数量。
NR当前文件记录数。
OFMT数字输出格式。
OFS输出域分隔符。
ORS输出记录分隔符。
RLENGTH通过match函数匹配的字符串的长度。
RS输入记录分隔符。
RSTART通过match函数匹配的字符串的偏移量。
SUBSEP下标分隔符。
   [root@xieqichao ~]# cat employees2
    Tom Jones:4424:5/12/66:543354
    Mary Adams:5346:11/4/63:28765
    Sally Chang:1654:7/22/54:650000
    Mary Black:1683:9/23/44:336500
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  [root@xieqichao ~]# awk -F: '{IGNORECASE = 1}; $1 == "mary adams" { print NR, $1, $2, $NF}' employees2
    2 Mary Adams 5346 28765
    [root@xieqichao ~]# awk -F: ' $1 == "mary adams" { print NR, $1, $2, $NF}' employees2
    没有输出结果。

 
 
  • 1
  • 2
  • 3
  • 4

当IGNORECASE内置变量的值为非0时,表示在进行字符串操作和处理正则表达式时关闭大小写敏感。这里的"mary adams"将匹配文件中的"Mary Admams"记录。最后print打印出第一、第二和最后一个域。需要说明的是NF表示当前记录域的数量,因此$NF将表示最后一个域的值。

awk在动作部分还提供了BEGIN块和END块。其中BEGIN动作块在awk处理任何输入文件行之前执行。事实上,BEGIN块可以在没有任何输入文件的条件下测试。因为在BEGIN块执行完毕以前awk将不读取任何输入文件。BEGIN块通常被用来改变内建变量的值,如OFS、RS或FS等。也可以用于初始化自定义变量值,或打印输出标题。

 [root@xieqichao ~]# awk 'BEGIN {FS = ":"; OFS = "\t"; ORS = "\n\n"} { print $1,$2,$3} filename

 
 
  • 1

上例中awk在处理文件之前,已经将域分隔符(FS)设置为冒号,输出文件域分隔符(OFS)设置为制表符,输出记录分隔符(ORS)被设置为两个换行符。BEGIN之后的动作模块中如果有多个语句,他们之间用分号分隔。
和BEGIN恰恰相反,END模块中的动作是在整个文件处理完毕之后被执行的。

  [root@xieqichao ~]# awk 'END {print "The number of the records is " NR }' filename

 
 
  • 1

awk在处理输入文件之后,执行END模块中的动作,上例中NR的值是读入的最后一个记录的记录号。

    [root@xieqichao ~]# awk '/Mary/{count++} END{print "Mary was found " count " times." }' employees2
    Mary was found 2 times.
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk '/Mary/{count++} END{print "Mary was found " count " times." }' employees2</span>
Mary was found 2 times.

<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># cat testfile</span>
northwest       NW      Charles Main                3.0     .98     3       34
western          WE      Sharon Gray                5.3     .97     5       23
southwest       SW      Lewis Dalsass              2.7     .8      2       18
southern         SO      Suan Chin                   5.1     .95     4       15
southeast        SE      Patricia Hemenway        4.0     .7      4       17
eastern           EA      TB Savage                   4.4     .84     5       20
northeast        NE      AM Main Jr.                  5.1     .94     3       13
north             NO       Margot Weber             4.5     .89     5       9
central           CT       Ann Stephens              5.7     .94     5       13

<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk '/^north/{count += 1; print count}' testfile     </span>
<span class="token comment">#如记录以正则north开头,则创建变量count同时增一,再输出其值。</span>
1
2
3

<span class="token comment">#这里只是输出前三个字段,其中第七个域先被赋值给变量x,在自减一,最后再同时打印出他们。</span>
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk 'NR &lt;= 3 {x = $7--; print "x = " x ", $7 = " $7}' testfile</span>
x <span class="token operator">=</span> 3, <span class="token variable">$7</span> <span class="token operator">=</span> 2
x <span class="token operator">=</span> 5, <span class="token variable">$7</span> <span class="token operator">=</span> 4
x <span class="token operator">=</span> 2, <span class="token variable">$7</span> <span class="token operator">=</span> 1    

<span class="token comment">#打印NR(记录号)的值在2--5之间的记录。</span>
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk 'NR == 2,NR == 5 {print "The record number is " NR}' testfile</span>
The record number is 2
The record number is 3
The record number is 4
The record number is 5

<span class="token comment">#打印环境变量USER和HOME的值。环境变量的值由父进程shell传递给awk程序的。</span>
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk 'BEGIN { print ENVIRON["USER"],ENVIRON["HOME"]}'</span>
root /root

<span class="token comment">#BEGIN块儿中对OFS内置变量重新赋值了,因此后面的输出域分隔符改为了\t。</span>
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk 'BEGIN { OFS = "\t"}; /^Sharon/{ print $1,$2,$7}' testfile</span>
western WE      5

<span class="token comment">#从输入文件中找到以north开头的记录count就加一,最后在END块中输出该变量。</span>
<span class="token punctuation">[</span>root@xieqichao ~<span class="token punctuation">]</span><span class="token comment"># awk '/^north/{count++}; END{print count}' testfile</span>
3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了354 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值