首页 > 代码库 > awk命令(2)

awk命令(2)

一、awk的正则表达式

1.正则表达式常见的用法

[A-Z]一个大写字母

[a-z]一个小写字母

[0-9]一个数字

[^A-Z]除了大写字母之外的一个字符

[^a-zA-Z]一个非英文字母的字符

[^a-zA-Z0-9]一个非英文字母、且非数字的字符

[a-z]\{3,5\}代表以小写字母组成的字符串,长度是3~5


2.查看tcp端口状态

[root@localhost ~]# netstat -ltn

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State      

tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN      

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      

tcp        0      0 0.0.0.0:817                 0.0.0.0:*                   LISTEN      

tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      

tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN      

tcp        0      0 :::5989                     :::*                        LISTEN      

tcp        0      0 :::22                       :::*                        LISTEN  


3.截取端口信息

[root@localhost ~]# netstat -ltn |awk ‘{print $4}‘ |awk -F: ‘/[0-9]$/ {print $NF}‘

2208

111

817

631

25

2207

5989

22


4.打印匹配mysql或者oracle的数据

[root@localhost ~]# awk ‘$0~/mysql|oracle/{print $0}‘ /etc/passwd

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

oracle:x:500:501::/home/oracle:/bin/bash



5.打印匹配m或o或r开始的数据

[root@localhost ~]# awk ‘$0~/^[mor]/{print $0}‘ /etc/passwd

root:x:0:0:root:/root:/bin/bash

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

oracle:x:500:501::/home/oracle:/bin/bash


6.打印非字母的数据

[root@localhost ~]# awk  ‘{if($0~/^[^a-zA-Z]/) print $0}‘ /etc/passwd



二、awk变量、判断和循环的简单用法

1,定义变量a="name is:",打印变量a和第一列

[root@localhost ~]# awk -F: ‘{a="name is:"} /root/ {print a,$1}‘ /etc/passwd

name is: root

name is: operator


2.begin定义变量,定义变量aa=0,匹配root就加1,完成之后再打印命令aa

[root@localhost ~]# awk ‘BEGIN{aa=0} /root/ {aa++} END{print aa}‘ /etc/passwd

2


3.if判断

[root@localhost ~]# awk -F: ‘{if($7!~"nologin") print $1,$7;else print NR,$NF}‘ /etc/passwd 


root /bin/bash

2 /sbin/nologin

sync /bin/sync

shutdown /sbin/shutdown

9 /sbin/nologin

32 /sbin/nologin

mysql /bin/bash

oracle /bin/bash

...


4.循环

if查看有两行

[root@localhost ~]# awk ‘{if($0~/root/) print $0}‘ /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin


while只查询一行就退出

[root@localhost ~]# awk ‘{while ($0~/root/) {print $0;exit}}‘ /etc/passwd

root:x:0:0:root:/root:/bin/bash



三、&&和||

[root@localhost shell]#tail -3 /etc/passwd |awk -F: ‘{if($0 ~ "27" && $0 ~ "bash") print $0 }‘

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

[root@localhost shell]# tail -3 /etc/passwd |awk -F: ‘{if($0 ~ "mysql" || $0 ~ "oracle") print $0 }‘

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

oracle:x:500:501::/home/oracle:/bin/bash


四、awk内置变量

RS:行分隔符

FS:列分隔符

OFS:定义列分隔符

ORS:定义行分隔符,默认是\n转行

FILENAME:文件名


FILENAME的用法

[root@localhost shell]#awk ‘BEGIN {FS=":"} {print NF,NR,$NF,$0}END{print "====================================================\n" FILENAME}‘ /etc/passwd

1 1 root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash

1 2 bin:x:1:1:bin:/bin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin

1 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin

1 4 adm:x:3:4:adm:/var/adm:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin

....

1 40 oracle:x:500:501::/home/oracle:/bin/bash oracle:x:500:501::/home/oracle:/bin/bash

====================================================

/etc/passwd


五、awk内置的字符串函数

1.gsub(r,s)在整个$0中用s替换r

[root@localhost ~]# awk ‘gsub ("root","bob"){print $0}‘ /etc/passwd

bob:x:0:0:bob:/bob:/bin/bash

operator:x:11:0:operator:/bob:/sbin/nologin


效果等同于sed的全局替换

[root@localhost ~]# sed -n ‘s/root/bob/gp‘  /etc/passwd

bob:x:0:0:bob:/bob:/bin/bash

operator:x:11:0:operator:/bob:/sbin/nologin



gsub(r,s,t)在整个t中用s替换r

[root@localhost ~]# awk ‘gsub ("root","bob",$2){print $0}‘ sedtest.txt

root bob root

root bob root


2.sub(r,s)用$0中最左边最长的子串代替s

[root@localhost ~]# awk ‘sub ("root","bob"){print $0}‘ /etc/passwd

bob:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/bob:/sbin/nologin


效果等同于sed局部替换

[root@localhost ~]# sed -n ‘s/root/bob/p‘  /etc/passwd

bob:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/bob:/sbin/nologin


3.index(s,t)返回s中字符串t的位置

[root@localhost ~]# awk ‘BEGIN{print index("I love you","ov")}‘

4


4.length(s)返回s长度

[root@localhost ~]# awk ‘BEGIN{print length("I love you")}‘

10


5.match(s,r)测试s是否包含匹配r的字符串

[root@localhost ~]# awk ‘BEGIN{print match("I love you","v")}‘

5

[root@localhost ~]# awk ‘BEGIN{print match("I love you","a")}‘

0


6.split(s,a,fs)在fs上将s分成序列a


7.substr(s,p)返回字符串s中从p开始的后缀部分


8.substr(s,p,n)返回字符串s中从p开始长度为n的后缀部分



六、awk内置数学函数

int(x)

sqrt(x)

exp(x)

log(x)

sin(x)

cos(x)


本文出自 “卡卡西” 博客,请务必保留此出处http://whnba.blog.51cto.com/1215711/1611014

awk命令(2)