2019-12-20
需要巧记,很多格式,学习难度:grep、sed、awk
awk知识概述
1三剑客awk命令介绍
2三剑客awk命令执行原理语法结构
3三剑客awk命令实操练习
查询替换信息排除(取反)
4三剑客awk命令高级功能
一awk命令概述
awk -pattern scanning and processing language
模式扫描(处理文件每一行信息)
过程语言(一门脚本语言、逻辑语句(循环/判断))
读取内容放到内存里面,而不是模式空间。模式就相当于sed的条件
命令语法结构:awk [参数] ‘模式{动作信息}’ 文件信息
作用说明:
1)擅长对文件列进行操作
2)擅长统计分析数据信息
[root@centos71 test]# cat awk.txt
#!/bin/awk
[root@centos71 test]# file awk.txt
awk.txt: awk script, ASCII text executable
[root@centos71 test]# cat python
#!/bin/python
[root@centos71 test]# file python
python: a /bin/python script, ASCII text executable
二awk命令操作
空格和制表符是有区别的
匹配空格时无法匹配制表符
对文件对齐
[root@centos71 test]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 41922560 3010792 38911768 8% /
devtmpfs 487064 0 487064 0% /dev
tmpfs 497960 0 497960 0% /dev/shm
tmpfs 497960 7924 490036 2% /run
tmpfs 497960 0 497960 0% /sys/fs/cgroup
/dev/sda1 201380 107080 94300 54% /boot
tmpfs 99596 0 99596 0% /run/user/0
[root@centos71 test]# df | column -t
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 41922560 3010792 38911768 8% /
devtmpfs 487064 0 487064 0% /dev
tmpfs 497960 0 497960 0% /dev/shm
tmpfs 497960 7924 490036 2% /run
tmpfs 497960 0 497960 0% /sys/fs/cgroup
/dev/sda1 201380 107080 94300 54% /boot
tmpfs 99596 0 99596 0% /run/user/0
(一)显示xiaoyu的姓氏和ID号码
根据条件找出相应行
[root@centos71 test]# cat awk.txt
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
[root@centos71 test]# awk '/Xiaoyu/{print $0}' awk.txt
Zhang Xiaoyu 390320151 :155:90:201
[root@centos71 test]# awk '/Xiaoyu/' awk.txt
Zhang Xiaoyu 390320151 :155:90:201
awk取出多列信息可以使用 , 或者 " " 进行分隔
注意只能使用双引号
[root@centos71 test]# awk '/Xiaoyu/{print $1" "$3}' awk.txt
Zhang 390320151
[root@centos71 test]# awk '/Xiaoyu/{print $1,$3}' awk.txt
Zhang 390320151
在系统中有时引号需要嵌入式使用时,不能使用相同:
[root@centos71 test]# awk '/Xiaoyu/{print $1' '$3}' awk.txt
awk: cmd. line:1: /Xiaoyu/{print $1
awk: cmd. line:1: ^ unexpected newline or end of string
$n~/xxx/取出第n列里面有xxx信息
~表示匹配,出现在awk或者搭建网站的配置文件里面
[root@centos71 test]# awk '$2~/Xiaoai/{print $1,$4}' awk.txt
Wang :50:95:135
(二)姓氏是zhang的人,显示他的第二次捐款金额及他的名字
根据条件找出相应行
[root@centos71 test]# awk '$1~/Zhang/' awk.txt
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
[root@centos71 test]#
[ :]+表示以空格或者冒号作为分隔符,一次或者多次
[root@centos71 test]# cat awk.txt
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75 Xiaoai
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
[root@centos71 test]# awk -F "[ :]+" '$1~/Zhang/{print $1,$2,$5}' awk.txt
Zhang Dandan 100
Zhang Xiaoyu 90
#| "表示#和空格是一个字符串了,整体识别
利用-F指定列分隔符,结合正则可以识别多个分隔符号
[root@centos61 ~]# awk -F "#| " '{print $1,$3}' /etc/selinux/config
This
SELINUX=
SELINUX=enforcing
SELINUXTYPE=
SELINUXTYPE=targeted
[root@centos61 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@centos61 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 3.7G 0 rom
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 48.8G 0 part /
├─sda3 8:3 0 19.5G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 2G 0 part [SWAP]
[root@centos61 ~]# lsblk | awk -F "[: ]+" '{print $1,$3}' | column -t
NAME MIN
sr0 0
sda 0
├─sda1 1
├─sda2 2
├─sda3 3
├─sda4 4
└─sda5 5
(三)显示所有以41开头的ID号码的人的全名和ID号码
简单,注意正则被三剑客所使用
[root@centos71 test]# awk '$3~/^41/' awk.txt
Zhang Dandan 41117397 :250:100:175
Liu Bingbing 41117483 :250:100:175
[root@centos71 test]# awk '$3~/^41/{print $1,$2,$3}' awk.txt
Zhang Dandan 41117397
Liu Bingbing 41117483
连续出现的字段可以使用高级方法
[root@centos71 test]# awk '{for(i=1;i<=3;i++) printf $i""FS;print ""}' awk.txt
Zhang Dandan 41117397
Zhang Xiaoyu 390320151
Meng Feixue 80042789
Wu Waiwai 70271111
Liu Bingbing 41117483
Wang Xiaoai 3515064655
Zi Gege 1986787350
Li Youjiu 918391635
Lao Nanhai 918391635
利用awk命令连续显示多列信息
[root@centos71 test]# awk '{for(i=1;i<=3;i++){print $i}}' num.txt
01
02
03
01
02
03
[root@centos71 test]# awk '{for(i=1;i<=3;i++){printf $i}}' num.txt
010203010203[root@centos71 test]#
换行显示
[root@centos71 test]# awk '{for(i=1;i<=3;i++){printf $i" "};printf "
"}' num.txt
01 02 03
01 02 03
(五)显示所有以5或者0结尾id显示出来,并显示人的名字和ID号码
awk '$3~/[05]$/{print $1,$2,$3}' awk.txt
Wang Xiaoai 3515064655
Zi Gege 1986787350
Li Youjiu 918391635
Lao Nanhai 918391635
[root@centos71 test]# awk '$3!~/[05]$/{print $1,$2,$3}' awk.txt
Zhang Dandan 41117397
Zhang Xiaoyu 390320151
Meng Feixue 80042789
Wu Waiwai 70271111
Liu Bingbing 41117483
[root@centos71 test]# which awk
/usr/bin/awk
[root@centos71 test]# ll /usr/bin/awk
lrwxrwxrwx. 1 root root 4 Nov 26 17:32 /usr/bin/awk -> gawk
(六)显示Xiaoyu的捐款,每个都以$开头
[root@centos71 test]# awk '$2~/Xiaoyu/{gsub(/:/,"$",$4);print $4}' awk.txt
$155$90$201
awk替换信息方法
gsub(/替换的信息/,"要替换成什么",$n替换的第几列信息)
二awk命令高级用法
awk模式概念:
普通模式: 利用正则进行匹配/利用行号进行匹配/利用字符进行匹配
特殊模式:
BEGIN: 在处理文件之前先做的事情 (准备工作)
END: 在处理文件之后要做的事情 (后续工作)
(一)在处理文件之前加表头
[root@centos71 test]# awk 'BEGIN{print"姓","名","id","金额"}{print $0}' awk.txt
姓 名 id 金额
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75 Xiaoai
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
[root@centos71 test]# awk 'BEGIN{print"姓","名","id","金额"}{print $0}' awk.txt | column -t
姓 名 id 金额
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75 Xiaoai
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
(二)进行四则运算
[root@centos71 ~]# awk 'BEGIN{print 2+2}'
4
[root@centos71 ~]# awk 'BEGIN{print 2-2}'
0
[root@centos71 ~]# awk 'BEGIN{print 2*2}'
4
[root@centos71 ~]# awk 'BEGIN{print 2/2}'
1
(三)进行变量设定
注意要加双引号,打印时不需要加$
[root@centos71 ~]# awk 'BEGIN{name=wang;print name}'
[root@centos71 ~]# awk 'BEGIN{name="wang";print name}'
wang
内置变量NR表示行号信息
[root@centos71 ~]# cat -n /etc/issue
1 S
2 Kernel
on an m
3
[root@centos71 ~]# awk '{print NR}' /etc/issue
1
2
3
NF:显示一行里面的字段数量
[root@centos61 ~]# cat /etc/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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
chrony:x:498:499::/var/lib/chrony:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@centos61 ~]# cat /etc/passwd | wc
22 28 983
[root@centos61 ~]# awk -F: '{print NF}' /etc/passwd | wc
22 22 44
FS : 表示分隔符字段信息
[root@centos61 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 50264772 910576 46794196 2% /
tmpfs 502068 0 502068 0% /dev/shm
/dev/sda3 20027260 44992 18958268 1% /app
/dev/sda1 999320 30508 916384 4% /boot
[root@centos61 ~]# df | awk -v FS=" " '{print $1,$5}'
Filesystem Use%
/dev/sda2 2%
tmpfs 0%
/dev/sda3 1%
/dev/sda1 4%
[root@centos61 ~]# df | awk -v FS=" " '{print $1,$5}' | column -t
Filesystem Use%
/dev/sda2 2%
tmpfs 0%
/dev/sda3 1%
/dev/sda1 4%
(四)END统计分析日志公式信息
[root@centos61 ~]# awk '/sbin/{i=i+1}END{print i}' /etc/passwd
21
[root@centos61 ~]# grep sbin /etc/passwd | wc
21 27 951
[root@centos71 ~]# grep "sbin" /etc/passwd -o
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
sbin
(五)END求和运算
求和公式: i=i+$n $
n 需要将第几列做求和运算
i=i+$1 i=0 i=0+1 print i--->1
i=i+$1 i=1 i=1+1 print i--->2
初始值为0
[root@centos61 ~]# seq 10 > seq.txt
[root@centos61 ~]# cat seq.txt
1
2
3
4
5
6
7
8
9
10
[root@centos61 ~]# awk '{i=i+$1; print i}' seq.txt
1
3
6
10
15
21
28
36
45
55
[root@centos61 ~]# awk '{i=i+$1}END{ print i}' seq.txt
55