awk命令
1. awk简介:
- awk是一个针对列的文本编辑器
- awk是一个强大的文本分析工具,awk就是把文件逐行的读入,是默认以空格作为空格符,将每行作为进行切片分割,然后再进行各种分析处理。
2. awk的使用方法
awk是依次对文件中的每一行进行处理,是以行作为单位来进行处理,然后通过执行相应的命令来处理文件。
- 格式实例:
使用awk查看文件的第一列.
awk '{print $1}' 要操作的文件
要查看第二列就将1改为2
3. awk命令用法
$0代表文件的全部内容
[root@node4 file]# cat 2
John Robinson 666-555-1111
[root@node4 file]# awk '{print $0}' 2
John Robinson 666-555-1111
因为awk输出内容需要有对象,这里echo一个空格然后交给awk去处理所以打印出了admin,而这里的1是因为前面定义的变量
[root@node4 file]# echo | awk 'BEGIN{i=1}{print "admin"}END{print i}'
admin
1
执行顺序是先执行BEGIN再依次执行后面的
3.1 使用awk命令打印一张表格
[root@node4 file]# awk 'BEGIN{print "姓名\t年龄\nadmin\t15\nkill\t18\ntom\t18"}'姓名 年龄
admin 15
kill 18
tom 18
// 模式匹配
[root@node4 file]# cat 1.awk
#!/usr/bin/awk
/[0-9]+/{print "这是数字"} //表示0-9中至少出现一个或者多个,就说明是数字,下面同理
/[a-zA-Z]+/{print "这是字符串"}
/^$/{print "这是空行"}
[root@node4 file]# awk -f 1.awk
1
这是数字
这是空行
4
这是数字
t
这是字符串
3.2 记录和字段
awk它将每个输入行作为一条记录,而将由空格或制表符分割的单词作为字段(用来分隔字段的字符被称为分隔符)
互换位置,将第三列放在这一行的最前面
[root@node4 file]# awk '{print $3,$1,$2}' all
666-555-1111 John Robinson
[root@node4 file]# cat all
John Robinson 666-555-1111
3.3 使用变量
将1和2赋值给ab,然后相加输出第三列的内容
[root@node4 file]# awk 'BEGIN{a=1;b=2}{print $(a+b)}' all
666-555-1111
-F选项
-F表示以什么作为分隔符,不加F默认以空格作为分隔符,这里以:,作为分割符,打印出第一列和第5列。也可以以其他的符号如/作为分割符
[root@node4 file]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@node4 file]# awk -F':' '{print $1,$5}' passwd
root root
bin bin
daemon daemon
也可以用数字作为分割符
[root@node4 file]# awk -F'[0-9]+' '{print $1}' passwd
root:x:
bin:x:
daemon:x:
3.4 使用awk命令取出IP地址
[root@node4 file]# ip a
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:23:e3:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.182.140/24 brd 192.168.182.255 scope global noprefixroute ens160
[root@node4 file]# ip a | grep -w 'inet' | grep -v '127.0.0.1' | awk -F'[ /]+' '{print $3}'
192.168.182.140
// FS定义系统变量来改变字段分隔符
[root@node4 file]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@node4 file]# awk 'BEGIN{FS=":"}{print $1,$6}' passwd
root /root
bin /bin
daemon /sbin
这里的OFS是当print要输出的列加逗号就是以空格作为分隔符,而此时OFS就会将空格换成-,但OFS默认也是空格作为分隔符,OFS也可以将其他的符号作为分割符
[root@node4 file]# awk 'BEGIN{FS=":";OFS="-"}{print $1,$6}' passwd
root-/root
bin-/bin
daemon-/sbin
3.5 模式匹配区域码
[root@node4 file]# cat all
707-724-0000
(275) 775-0000
(754)724-0000
1-708-725-0000
1 707-724-0000
1(707)725-0000
/1?表示1出现0次或者一次,(-| )?表示出现-或者空格至少一次,\(?[0-9]\)?表示的意思是(不出现或至少出现一次[0-9]表示括号0-9的数字出现一个或者多个。后面出现的前面都出现了就不再过多说明了
[root@node4 file]# awk '/1?(-| )?\(?[0-9]+\)?( |-)?[0-9]+-[0-9]+/' all
707-724-0000
(275) 775-0000
(754)724-0000
1-708-725-0000
1 707-724-0000
1(707)725-0000
[root@node4 file]# awk '/^\([0-9]+\) ?[0-9]+-[0-9]+/' all
(275) 775-0000
(754)724-0000
3.6 统计空行数
++x在返回结果前递增x的值(前缀)先得到结果再去加
x++在返回结果之后x的值(后缀)先加再得到结果
[root@node4 file]# cat 2
the is 1
the is 2
the is 3
[root@node4 file]# awk '/^$/{print y += 1}' 2
1
2
3
4
统计is出现的次数
[root@node4 file]# cat 2
the is 1
the is 2
the is 3
[root@node4 file]# awk '/is/{print y += 1}' 2
1
2
3
3.7 求和
[root@node4 file]# cat 2
tom 99 98 75 88 90
admin 100 78 97 75 70
kill 98 95 96 88 99
[root@node4 file]# awk '{num=$2+$3+$4+$5+$6;print num}' 2
450
420
476
3.8 求平均值
[root@node4 file]# cat 2
tom 99 98 75 88 90
admin 100 78 97 75 70
kill 98 95 96 88 99
[root@node4 file]# awk '{num=$2+$3+$4+$5+$6;print num/5}' 2
90
84
95.2
[root@node4 file]# awk '{num=$2+$3+$4+$5+$6;avg=num/5;print $1,avg}' 2 tom 90
admin 84
kill 95.2
3.9 NR的使用
[root@node4 file]# cat 2
tom 99 98 75 88 90
admin 100 78 97 75 70
kill 98 95 96 88 99
[root@node4 file]# awk '{print NR".",$1}' 2
1. tom
2. admin
3. kill
awk的简单应用
[root@node4 file]# cat test.sh
#!/bin/bash
awk '{print NR".",$1,$2"元"}' 3
read -p "请输入您要购买的商品:" yes
read -p "请输入您需要的数量:" meny
awk -vnum=$meny -vcolumn=$yes 'NR==column{print "购买了"num"瓶"$1",一共"$2*num"元"}' 3
[root@node4 file]# cat 3
农夫山泉 2
优乐美 3
香飘飘 4
雀巢 6