linux shell-awk样例2

awk.sh

#!/bin/bash

for file in $@
do
 if [ -f $file ];then
   echo "File is:" $file
   awk 'BEGIN {print "The count of \"tecmint.com\" is:"} /^tecmint.com/{counter+=1} END {printf "%s\n",counter}' $file
 else
   echo "$file is not a file,please input one file." >&2 && exit 1
 fi
done
exit 0
#$@:显示所有向脚本传递的参数。
#BEGIN模式:是指awk将在读取任何输入行之前立即执行BEGIN中指定的动作。
#END模式:是指awk将在它正式退出前执行END中指定的动作。
#上述awk命令脚本的执行流程如下:
#当在脚本中使用了BEGIN模式,则BEGIN中所有的动作都会在读取任何输入行之前执行。然后,读入一个输入行并解析成不同的段。接下来,每一条指定的非特殊模式都会和输入行进行比较匹配,当匹配成功后,就会执行模式对应的动作。对所有你指定的模式重复此执行该步骤,当读取并处理完所有输入行后,假如你指定了END模式,那么将会执行相应的动作。

验证:

[root@logstash ~]# sh awk.sh 
[root@logstash ~]# sh awk.sh domains.txt 
File is: domains.txt
The count of "tecmint.com" is:
6
[root@logstash ~]# cat domains.txt 
news.tecmint.com
tecmint.com
linuxsay.com
windows.tecmint.com
tecmint.com
news.tecmint.com
tecmint.com
linuxsay.com
tecmint.com
news.tecmint.com
tecmint.com
linuxsay.com
windows.tecmint.com
tecmint.com
[root@logstash ~]# 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小黑要上天

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

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

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

打赏作者

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

抵扣说明:

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

余额充值