awk报告生成器

1.awk处理机制:awk会逐行处理文本,支持在处理第一行之前做一些准备工作,以及在处理完最后一行最一些总结性质的工作,在命令格式上分别体现如下:
BEGIN{}:读入第一行文本之前执行,一般用来初始化操作
{}:逐行处理,逐行读入文本执行相应的处理,是最常见的编辑指令
END{}:处理完最后一行文本后执行,一般用来输出处理结果
2.awk基本用法
linux上面默认使用gawk
awk ‘{print FILENAME}’ passwd
awk ‘{print 第"NR"行,有“NF”列}’
awk ‘BEGIM{print NAME}’
awk ‘END{print WESTOS}’
awk -F : ‘BEGIN{print NAME}{printKaTeX parse error: Expected ‘EOF’, got ‘}’ at position 2: 1}̲END{WESTOS}’ aw…/’
awk -F : '/bash$/{print$1}'

  • 实验1

1.实验的前提条件:

[root@shell_example ~]# cd /mnt/
[root@shell_example mnt]# ls
[root@shell_example mnt]# cp /etc/passwd .
[root@shell_example mnt]# vim passwd 

在这里插入图片描述

2.初始化操作,使其在最先开始的时候输入“hello”

[root@shell_example mnt]# awk -F : 'BEGIN{print "hello"}'

在这里插入图片描述
3.输出passwd文件中每一行的第一个字符,并且在最开始的时候输出“hello”,在结束的时候输出“end”

[root@shell_example mnt]# awk -F : 'BEGIN{print "hello"}{print $1}END{print"end"}' passwd

在这里插入图片描述

4.输出文件名

[root@shell_example mnt]# awk -F : '{print FILENAME}' passwd

在这里插入图片描述

5.输出passwd文件的行号

[root@shell_example mnt]# awk -F : '{print NR}' passwd

在这里插入图片描述

6.输出passwd文件的列数

[root@shell_example mnt]# awk -F : '{print NF}' passwd

在这里插入图片描述

7.输出passwd文件中每一行的第一列内容

[root@shell_example mnt]# awk -F : '{print $1}' passwd

在这里插入图片描述

8.输出passwd文件中以bash结尾的行,并只输出该行的第一列内容

[root@shell_example mnt]# awk -F : '/bash$/{print $1}' passwd

在这里插入图片描述

9.输出passwd文件中不以bash结尾的行,并只输出该行的第一列内容

[root@shell_example mnt]# awk -F : '!/bash$/{print $1}' passwd

在这里插入图片描述

练习:输出passwd文件中用户可以登陆成功的个数

[root@shell_example mnt]# awk 'BEGIN{N=0}/bash$/{N++}END{print N}' passwd

在这里插入图片描述

注意:1.如果不写空格符,默认的是空格
2.awk是逐行处理,每一行处理一次
3.不加引号,默认的的是执行的动作

  • 实验2

1.当N=100时,N+50=?

[root@shell_example mnt]# awk 'BEGIN{N=100;print N+50}'

在这里插入图片描述

2.输出passwd文件中每行有a-d中任意一个字符的行

[root@shell_example mnt]# awk '/[a-d]/{print}' passwd

在这里插入图片描述

3.输出passwd文件中以a-d中任意一个字符开头的行

[root@shell_example mnt]# awk '/^[a-d]/{print}' passwd

在这里插入图片描述

4.输出passwd文件中不以a-d中任意一个字符开头的行

[root@shell_example mnt]# awk '!/^[a-d]/{print}' passwd

在这里插入图片描述

5.输出passwd文件中不以a-d中任意一个字符开头的行

[root@shell_example mnt]# awk '/^[^a-d]/{print}' passwd

在这里插入图片描述

6.输出passwd文件中以a-d中任意一个字符开头的行

[root@shell_example mnt]# awk '/^[!a-d]/{print}' passwd

在这里插入图片描述

7.输出passwd文件中以a字符开头或以bash结尾的行

[root@shell_example mnt]# awk '/^a|bash$/{print}' passwd

在这里插入图片描述

8.输出passwd文件中不以a字符开头和不以bash结尾的行

[root@shell_example mnt]# awk '!/^a|bash$/{print}' passwd

在这里插入图片描述

9.输出passwd文件中第六列是bin结尾的行

[root@shell_example mnt]# awk -F : '$6~/\<bin$/' passwd

在这里插入图片描述

10.输出passwd文件中第六列不是bin结尾的行

[root@shell_example mnt]# awk -F : '$6!~/\<bin$/' passwd

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值