AWK笔记

1.指定awk的分隔符,默认awk取列的时候,会用空格隔开每一列,这个空格是awk的默认输出分隔符
我们可以使用awk的内置变量OFS来设定awk的输出分隔符,配合-v参数,实例如下:

[root@centos tmp]# awk '{print $1,$2}' test 
abc 123
8ua 456
[root@centos tmp]# awk -v OFS="+++" '{print $1,$2}' test 
abc+++123
8ua+++456

2.两列想合并在一起显示,可以用下面实现

[root@centos tmp]# awk '{print $1 $2}' test 
abc123
8ua456

3.利用NR内置变量,显示每行有多少列

[root@centos tmp]# cat test 
abc	123	iuy	ddd
8ua	456	auv	ppp	7y7
[root@centos tmp]# awk '{print NR,NF}' test 
1 4
2 5

4.利用NR内置变量,先打印行号,在打印整行内容

[root@centos tmp]# awk '{print NR $0}' test 
1abc	123	iuy	ddd
28ua	456	auv	ppp	7y7

5.FNR内置变量
使用awk处理多个文件,并且使用NR显示行号,效果如下

[root@centos tmp]# cat test 
abc	123	iuy	ddd
8ua	456	auv	ppp	7y7
[root@centos tmp]# cat qtest 
abc#123#iuy#ddd
8ua#456#auv#ppp#7y7
[root@centos tmp]# awk '{print NR,$0}' test qtest 
1 abc	123	iuy	ddd
2 8ua	456	auv	ppp	7y7
3 abc#123#iuy#ddd
4 8ua#456#auv#ppp#7y7

如果我们想分别显示两个文件的行号,就要用到FNR内置变量

[root@centos tmp]# awk '{print FNR,$0}' test qtest 
1 abc	123	iuy	ddd
2 8ua	456	auv	ppp	7y7
1 abc#123#iuy#ddd
2 8ua#456#auv#ppp#7y7

6.内置变量RS
RS是输入行分隔符,不指定,默认的行分隔符就好似我们所理解的回车换行
这里如何让awk遇到一个空格,就换行,示列如下:

[root@centos tmp]# cat test 
abc 123	iuy ddd
8ua 456	auv ppp 7y7
[root@centos tmp]# awk '{print NR,$0}' test 
1 abc 123	iuy ddd
2 8ua 456	auv ppp 7y7
[root@centos tmp]# awk -v RS=" " '{print NR,$0}' test 
1 abc
2 123	iuy
3 ddd
8ua
4 456	auv
5 ppp
6 7y7

7.内置变量ORS
设置"+++"为真正的输出行分隔符,默认输出行分隔符是回车换行,就是把换行变成+++

[root@centos tmp]# cat test 
abc 123	iuy ddd
8ua 456	auv ppp 7y7
[root@centos tmp]# awk -v ORS="+++" '{print NR,$0}' test 
1 abc 123	iuy ddd+++2 8ua 456	auv ppp 7y7+++[root@centos tmp]#

8.内置变量FILENAME
内置变量FILENAME意思是显示文件名

[root@centos tmp]# awk '{print FILENAME,FNR,$0}' test qtest 
test 1 abc 123	iuy ddd
test 2 8ua 456	auv ppp 7y7
qtest 1 abc#123#iuy#ddd
qtest 2 8ua#456#auv#ppp#7y7

9.内置变量ARGC与ARGV
ARGV变量是一个数组,下标1对应是后面第一个文件的名字

[root@centos tmp]# awk 'BEGIN{print "AA",ARGV[0],ARGV[1]}' test 
AA awk test
内置变量ARGC表示参数的数量,也可以理解ARGV数组的长度
[root@centos tmp]# awk 'BEGIN{print "AA",ARGV[0],ARGV[1],ARGC}' test 
AA awk test 2

10.自定义变量

方法1:-v varname=value 变量名区分字符大小写
方法2:在prigram中直接定义

[root@centos tmp]# awk -v myVar="testVar" 'BEGIN{print myVar}'
testVar

[root@centos tmp]# awk 'BEGIN{myVar="ttt" ; print myVar}'
ttt

11.printf

[root@centos tmp]# printf "%s\n" abc def ghi jkl
abc
def
ghi
jkl

多个格式替换符

printf "%s %s\n" abc def ghi jkl
abc def
ghi jkl

格式如下:

printf format item1 item2 ...
printf "%s\n" abc def ...
%s相当于每一个文本

12.awk的正则

匹配g开头的账户名

[root@server ~]# awk '/^g/{print $0}' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin

匹配两正则中间的内容

[root@server ~]# awk '/^g/,/^a/{print $0}' /etc/passwd
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

匹配有重复字符时:

[root@server tmp]# cat test 
hey
heey
heeey
heeeey
[root@server tmp]# awk --posix '/he{2,3}y/{print $0}' test   #表示e可以出现最少2次,最多三次
heey
heeey

13.awk动作

案例1:

[root@server tmp]# cat test 
f s
1 2
1 2
[root@server tmp]# awk '{ if(NR==1){print $0}}' test 
f s

案例2:

[root@server tmp]# cat test 
姓名 年龄
郭心心 18
蒋磊磊 66
曲公公 36
[root@server tmp]# awk 'NR!=1 {if($2<20){print $1,"年轻人"} else if($2>20 && $2<40){print $1,"中年人"}else{print $1,"老年人"}}' test 
郭心心 年轻人
蒋磊磊 老年人
曲公公 中年人

14.awk数组统计

[root@server tmp]# cat test 
192.168.1.1
192.168.1.1
192.168.1.2
192.168.1.4
192.168.1.5
192.168.1.5
192.168.1.2
192.168.1.2
192.168.1.7
192.168.1.5
192.168.1.2
[root@server tmp]# awk '{count[$1]++} END{for(i in count){print i,count[i]}}' test 
192.168.1.4 1
192.168.1.5 3
192.168.1.7 1
192.168.1.1 2
192.168.1.2 4

15.split动态创建数组

[root@server tmp]# awk -v "ts=suixin:xiaoxinzi:xiaojiangzi" 'BEGIN{split(ts,huua,":");for(i in huua){print huua[i]}}'
suixin
xiaoxinzi
xiaojiangzi

16.打印奇偶行

[root@server tmp]# cat test 
第 1 行
第 2 行
第 3 行
第 4 行
第 5 行
[root@server tmp]# awk 'i=!i' test 
第 1 行
第 3 行
第 5 行
[root@server tmp]# awk '!(i=!i)' test 
第 2 行
第 4 行

参考网址:http://www.zsythink.net/archives/tag/awk/

posted @ 2022-04-15 20:48  彬彬l  阅读(55)  评论(0)    收藏  举报