awk介绍

awk是一种编程语言,主要用于在linux/unix下对文本和数据进行处理。

数据可以来自标准输入、一个或多个文件,或其它命令的输出。

awk的处理文本和数据的方式:逐行扫描文件,默认从第一行到最后一行,寻

找匹配的特定模式的行,并在这些行上进行你想要的操作。

awk使用方式

命令模式

语法

awk  'commands' 

常用选项

-v  
-F
-F -F"分隔符"
-F"[分隔符1分隔符2...]"
-F"[分隔符1分隔符2...]+"

'命令部分(commands)'

引用shell变量时需用双引号引起

1、通过 正则表达式,地址定位 去匹配特定模式的行进行列操作

'/root/{awk语句}'           sed '/root/p'
'NR==1,NR==5{awk语句}' sed '1,5p'
'/^root/,/^ftp/{awk语句}' sed'/^root/,/^ftp/p'

2、{awk语句1;awk语句2;...}

'{print $0;print $1}'     sed'p'
'NR==5{print $0}' sed'5p'
awk

3、BEGIN...END....

'BEGIN{awk语句};{处理中};END{awk语句}'
'BEGIN{awk语句};{处理中}'
'{处理中};END{awk语句}'

脚本模式

脚本编写

#!/bin/awk -f     定义魔法字符

awk \n
BEGIN{FS=":"}
NR==1,NR==3{print $1"\t"$NF}
...

脚本执行

1
awk -f awk
awk -f awk.sh filename awk shell


sed -f sed.sh -i filename sed


2
./awk()
chmod +x awk.sh
./awk.sh filename


./sed.sh -i filename

awk内部相关变量

$0              
$1,$2...$n n awk -F: '{print $1,$3}'
NF awk -F: '{print NF}'
$NF $(NF-1)
FNR/NR

FS 'BEGIN{FS=":"};{print $1,$3}'
OFS 'BEGIN{OFS="\t"};print $1,$3}'
RS awk 'BEGIN{RS="\t"};{print $0}'
ORS 'BEGIN{ORS="\n\n"};{print $1,$3}'

FILENAME

示例1


[root@bobilinux tmp]# cat pass
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash


# 打印用户名、用户家目录和默认shell
[root@bobilinux tmp]# awk -F: '{print $1,$(NF-1),$NF}' pass
ett /home/ett /bin/bash
rpc /var/lib/rpcbind /sbin/nologin
rpcuser /var/lib/nfs /sbin/nologin
nfsnobody /var/lib/nfs /sbin/nologin
user01 /home/user01 /bin/bash
user02 /home/user02 /bin/bash
user03 /home/user03 /bin/bash
user04 /home/user04 /bin/bash
user05 /home/user05 /bin/bash
yy1 /home/yy1 /bin/bash

# 打印列数
[root@bobilinux tmp]# awk -F: '{print NF}' pass #awk按行处理,每一行都打印列数
7
7
7
7
7
7
7
7
7
7



# 打印包含 ett 的行
[root@bobilinux tmp]# awk '/ett/{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
[root@bobilinux tmp]# awk '/ett/' pass
ett:x:821:821::/home/ett:/bin/bash
[root@bobilinux tmp]# awk '/ett/{print $1}' pass # 默认以空格分隔
ett:x:821:821::/home/ett:/bin/bash

# 打印包含 ett 的行的用户名和默认shell
[root@bobilinux tmp]# awk -F: '/ett/{print $1,$NF}' pass
ett /bin/bash

# 打印以 ett 开头的行到以 rpcuser 开头的行中的用户名和默认shell
[root@bobilinux tmp]# awk -F: '/^ett/,/^rpcuser/{print $1,$NF}' pass
ett /bin/bash
rpc /sbin/nologin
rpcuser /sbin/nologin


使 "分隔符"
# 指定输出结果的分隔符
[root@bobilinux tmp]# awk -F: '/^ett/,/^rpcuser/{print $1" 的默认shell是 "$NF}' pass
ett shell /bin/bash
rpc shell /sbin/nologin
rpcuser shell /sbin/nologin



[root@bobilinux tmp]# awk 'NR==1,NR==5{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash


# 地址定位+正则表达式

[root@bobilinux tmp]# awk 'NR==1,NR==5/^ett/{print $0}' pass
awk: NR==1,NR==5/^ett/{print $0}
awk: ^ syntax error

[root@bobilinux tmp]# awk 'NR==1,NR==5;/^ett/{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash

示例2

-FBEGINFS
# 指定分隔符
[root@bobilinux tmp]# awk 'BEGIN{FS=":"};/^ett/,/^rpcuser/{print $1,$NF}' pass
ett /bin/bash
rpc /sbin/nologin
rpcuser /sbin/nologin


awk""BEGINOFS
# 指定输出结果的分隔符
[root@bobilinux tmp]# awk -F: 'BEGIN{OFS="***********"};/^ett/,/^rpcuser/{print $1,$NF}' pass
ett***********/bin/bash
rpc***********/sbin/nologin
rpcuser***********/sbin/nologin


awk
[root@bobilinux tmp]# vim pass
ett:x:821:821::/home/ett:/bin/bash hello ^Iworld$
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin^Ite^Ist$
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin$
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin$
user01:x:822:822::/home/user01:/bin/bash$
user02:x:823:823::/home/user02:/bin/bash$
user03:x:824:824::/home/user03:/bin/bash$
user04:x:825:825::/home/user04:/bin/bash$
user05:x:826:826::/home/user05:/bin/bash$
yy1:x:827:827::/home/yy1:/bin/bash$

[root@bobilinux tmp]# awk '{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash hello world
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin te st
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash

[root@bobilinux tmp]# awk 'BEGIN{RS="\t"};{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash hello
world
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
te
st
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash


awk
[root@bobilinux tmp]# awk '{print $0}' pass
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash
[root@bobilinux tmp]# awk 'BEGIN{ORS="*****"};{print $0}' pass
user02:x:823:823::/home/user02:/bin/bash*****user03:x:824:824::/home/user03:/bin/bash*****user04:x:825:825::/home/user04:/bin/bash*****user05:x:826:826::/home/user05:/bin/bash*****yy1:x:827:827::/home/yy1:/bin/bash*****[root@bobilinux tmp]# awk 'BEGIN{ORS="\n\n"};{print $0}' pass
user02:x:823:823::/home/user02:/bin/bash

user03:x:824:824::/home/user03:/bin/bash

user04:x:825:825::/home/user04:/bin/bash

user05:x:826:826::/home/user05:/bin/bash

yy1:x:827:827::/home/yy1:/bin/bash

[root@bobilinux tmp]#

示例3


[root@bobilinux tmp]# cat pass
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash

[root@bobilinux tmp]# awk '{print FILENAME}' pass
pass
pass
pass



[root@bobilinux tmp]# cat pass
user04:x:825:825:@:/home/user04:@/bin/bash
user05:x:@826:826::/home/user05:/bin/bash
yy1:x:827:827@::/home/yy1:/bin/bash
[root@bobilinux tmp]#
[root@bobilinux tmp]# awk 'BEGIN{RS="@"};{print $0}' pass
user04:x:825:825:
:/home/user04:
/bin/bash
user05:x:
826:826::/home/user05:/bin/bash
yy1:x:827:827
::/home/yy1:/bin/bash

[root@bobilinux tmp]# awk 'BEGIN{RS="@";ORS="*"};{print $0}' pass
user04:x:825:825:*:/home/user04:*/bin/bash
user05:x:*826:826::/home/user05:/bin/bash
yy1:x:827:827*::/home/yy1:/bin/bash
*[root@bobilinux tmp]#

awk工作原理

awk -F: '{print $1,$3}' /etc/passwd

1awk使$0(RS)
2:()()$1
awk
FSFS
3awk使print$1,$3OFSOFS
4awk$0

awk变量定义

语法

awk -v 变量名=变量值 '{awk语句}' 文件

awk -v 变量名=变量值 'BEGIN{awk语句}'

实例

awk$

[root@bobilinux tmp]# awk -v num=1 '{print num}' pass
1
1
1
[root@bobilinux tmp]# awk -v num=1 '{print $num}' pass
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash


[root@bobilinux tmp]# awk -v num=1 'BEGIN{print num}'
1
[root@bobilinux tmp]# awk -v num=1 'BEGIN{print $num}'

[root@bobilinux tmp]#

awk中BEGIN...END使用

BEGIN:表示在程序开始前执行

END :表示所有文件处理完后执行

用法

'BEGIN{开始处理之前};{处理中};END{处理结束后}'

实例

1、打印最后一列和倒数第二列(登录shell和家目录)

[root@bobilinux tmp]# awk -F: 'BEGIN{print "Login_shell\t\tLogin_home\n*************************************"};{print $NF"\t\t"$(NF-1)};END{print "*************************************"}' pass 
Login_shell Login_home
*************************************
/bin/bash /home/user02
/bin/bash /home/user03
/bin/bash /home/user04
/bin/bash /home/user05
/bin/bash /home/yy1
*************************************


[root@bobilinux tmp]# awk 'BEGIN{FS=":";print "Login_shell\t\tLogin_home\n*************************************"};{print $NF"\t\t"$(NF-1)};END{print "*************************************"}' pass
Login_shell Login_home
*************************************
/bin/bash /home/user02
/bin/bash /home/user03
/bin/bash /home/user04
/bin/bash /home/user05
/bin/bash /home/yy1
*************************************

2、打印/etc/passwd里的用户名、家目录及登录shell

[root@bobilinux tmp]# awk -F: 'BEGIN{OFS="\t";print "u_name\th_dir\tshell\n*********************************"};{print $1,$(NF-1),$NF};END{print "*********************************"}' pass
u_name h_dir shell
*********************************
user02 /home/user02 /bin/bash
user03 /home/user03 /bin/bash
user04 /home/user04 /bin/bash
user05 /home/user05 /bin/bash
yy1 /home/yy1 /bin/bash
*********************************



[root@bobilinux tmp]# awk -F: 'BEGIN{print "u_name\th_dir\tshell\n*********************************"};{printf "%-15s%-20s%-20s\n",$1,$(NF-1),$NF};END{print "*********************************"}' pass
u_name h_dir shell
*********************************
user02 /home/user02 /bin/bash
user03 /home/user03 /bin/bash
user04 /home/user04 /bin/bash
user05 /home/user05 /bin/bash
yy1 /home/yy1 /bin/bash
*********************************

awk格式化输出

print函数  

类似echo

   print "字符串"

[root@bobilinux tmp]# date|awk '{print "Month: "$2"\nYear: "$NF}'
Month: May
Year: 2022

printf函数

 类似echo -n

%d  
%s strings
%-20s
20
-
printf \n

printf "%s %s...\n",12...

{printf "%-15s %-20s %-20s\n",$1,$(NF-1),$NF}

[root@bobilinux tmp]# awk -F: '{printf "%-15s %-10s %-15s\n", $1,$2,$3}' pass
user04 x 825
user05 x @826
yy1 x 827

[root@bobilinux tmp]# awk -F: '{printf "|%15s| %10s| %15s|\n", $1,$2,$3}' pass
| user04| x| 825|
| user05| x| @826|
| yy1| x| 827|
[root@bobilinux tmp]# awk -F: '{printf "|%15s| %10s| %15s|", $1,$2,$3}' pass
| user04| x| 825|| user05| x| @826|| yy1| x| 827|[root@bobilinux tmp]#

awk和正则的综合运用

逻辑运算符

==       
!=
>
<
>
<=
~
!~
!
&&
||

参考文件:

[root@bobilinux tmp]# cat pass 
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash

实例

nfs
[root@bobilinux tmp]# awk 'NR==1,/^nfs/{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin



3
[root@bobilinux tmp]# awk 'NR==1,NR==3{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

[root@bobilinux tmp]# awk 'NR>=1&&NR<=3{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin



ettnsf
[root@bobilinux tmp]# awk '/^ett/,/^nfs/{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin



ettnsf
[root@bobilinux tmp]# awk '/^ett/||/^nfs/{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@bobilinux tmp]# awk '/^ett|^nfs/{print $0}' pass
ett:x:821:821::/home/ett:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@bobilinux tmp]# awk '/^ett/;/^nfs/{print $0}' pass
ett:x:821:821
::/home/ett:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@bobilinux tmp]# grep -E '^ett|^nfs' pass
ett:x:821:821::/home/ett:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@bobilinux tmp]# sed -nr '/^ett|^nfs/p' pass
ett:x:821:821::/home/ett:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

[root@bobilinux tmp]# sed -n '/^ett/p;/^nfs/p' pass
ett:x:821:821::/home/ett:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin



1-5bash
[root@bobilinux tmp]# awk 'NR>=1 && NR<=5 && /bash$/' pass
ett:x:821:821::/home/ett:/bin/bash
user01:x:822:822::/home/user01:/bin/bash



;||
||
[root@bobilinux tmp]# awk 'NR>=3 && NR<=6 || /bash$/' pass
ett:x:821:821::/home/ett:/bin/bash
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash

;
[root@bobilinux tmp]# awk 'NR>=3 && NR<=6 ; /bash$/' pass
ett:x:821:821::/home/ett:/bin/bash
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
user01:x:822:822::/home/user01:/bin/bash
user01:x:822:822::/home/user01:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user02:x:823:823::/home/user02:/bin/bash
user03:x:824:824::/home/user03:/bin/bash
user04:x:825:825::/home/user04:/bin/bash
user05:x:826:826::/home/user05:/bin/bash
yy1:x:827:827::/home/yy1:/bin/bash



IP
[root@bobilinux tmp]# ifconfig eth0|awk '/inet addr/'|awk -F: '{print $2}'|awk '{print $1}'
192.168.192.131

[root@bobilinux tmp]# ifconfig eth0|awk -F"[: ]+" '/inet addr/{print $4}'
192.168.192.131

[root@bobilinux tmp]# ifconfig eth0|awk 'NR==2'|awk -F"[: ]+" '{print $4,$6,$8}'
192.168.192.131 192.168.192.255 255.255.255.0

[root@bobilinux tmp]# ifconfig eth0|awk 'NR==2'|awk -F"[: ]+" 'BEGIN{OFS="\n"};{print $4,$6,$8}'
192.168.192.131
192.168.192.255
255.255.255.0

awk的脚本编程

awk 选项 '正则,地址定位{awk语句}'  文件名

处理文件:'{语句}' 后面接文件

不处理文件:'BEGIN{语句}'

流程控制语句

if:

{if(){1;2;...}}


[root@bobilinux tmp]# awk -F: '{if($3==0) {print $1"是管理员"} }' /etc/passwd
root

[root@bobilinux tmp]# awk 'BEGIN{if($(id -u)==0) {print "root"}}'
root

if...else:

{if(){;;...}else{;;...}}


[root@bobilinux tmp]# awk -F: '{ if($3>=500 && $3 != 65534) {print $1"是普通用户"} else {print $1,"不是普通用户"}}' /etc/passwd
root
bin
user04
user05
yy1


[root@bobilinux tmp]# awk 'BEGIN{if( $(id -u)>=500 && $(id -u) !=65534 ) {print "是普通用户"} else {print "不是普通用户"}}'

if...else if...else:

{if(1){;...}else if(2){;...}else if(3){;...}else{;...}}


[root@bobilinux tmp]# awk -F: '{if($3==0){print $1,"是管理员"}else if($3>=1 && $3<=499 || $3==65534){print $1,"是系统用户"}else{print $1,"是普通用户"}}' /etc/passwd
root
bin
daemon
user04
user05
yy1

[root@bobilinux tmp]# awk -F: '{ if($3==0) {i++} else if($3>=1 && $3<=499 || $3==65534 ) {j++} else {k++}};END{print "管理员个数为:"i "\n系统用户个数为:"j"\n普通用户的个数为:"k }' /etc/passwd
:1
:27
:24

循环语句

while:

[root@bobilinux tmp]# i=1;while (($i <= 5));do echo $i;let i++;done
1
2
3
4
5


[root@bobilinux tmp]# awk 'BEGIN{i=1;while(i<=5) {print i;i++}}'
1
2
3
4
5


3
[root@bobilinux tmp]# awk -F: '{i=1;while(i<=3) {print $0;i++}}' pass
ett:x:821:821::/home/ett:/bin/bash
ett:x:821:821::/home/ett:/bin/bash
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin


1-5
[root@bobilinux tmp]# awk 'BEGIN { i=1;while(i<=5) {(sum+=i) i++};{print sum}}'
15

for:

[root@bobilinux tmp]# for((i=1;i<=5;i++));do echo $i;done
1
2
3
4
5


[root@bobilinux tmp]# awk 'BEGIN{for(i=1;i<=5;i++) {print i}}'
1
2
3
4
5


3
[root@bobilinux tmp]# awk -F: '{for(i=1;i<=3;i++) {print $0}}' pass
ett:x:821:821::/home/ett:/bin/bash
ett:x:821:821::/home/ett:/bin/bash
ett:x:821:821::/home/ett:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin


1-5
[root@bobilinux tmp]# awk 'BEGIN{for(i=1;i<=5;i++) (sum+=i);{print sum}}'
15


[root@bobilinux tmp]# for ((i=1;i<=10;i+=2));do echo $i;done|awk -v sum=0 '{sum+=$0};END{print sum}'
25

嵌套循环:

#!/bin/bash
for ((y=1;y<=5;y++))
do
for ((x=1;x<=$y;x++))
do
echo -n $x
done
echo
done

1
12
123
1234
12345


[root@bobilinux tmp]# awk 'BEGIN{for(y=1;y<=5;y++) {for(x=1;x<=y;x++) {printf x};print}}'
1
12
123
1234
12345


[root@bobilinux tmp]# awk 'BEGIN{y=1;while(y<=5) {for(x=1;x<=y;x++) {printf x};print;y++}}'
1
12
123
1234
12345

循环的控制:

break     
continue


[root@bobilinux tmp]# awk 'BEGIN{for(i=1;i<=5;i++) {if(i==3) break;print i} }'
1
2
[root@bobilinux tmp]# awk 'BEGIN{for(i=1;i<=5;i++){if(i==3) continue;print i}}'
1
2
4
5


[root@bobilinux tmp]# awk 'BEGIN{i=1;while(i<=5){if(i==3) break;print i;i++}}'
1
2
[root@bobilinux tmp]# awk 'BEGIN{i=0;while(i<5){i++;if(i==3) continue;print i}}'
1
2
4
5

awk算数运算

+ - * / %() ^(2^3)
awkshell


[root@bobilinux tmp]# awk 'BEGIN{print 1+1}'
2
[root@bobilinux tmp]# awk 'BEGIN{print 1+1.5}'
2.5
[root@bobilinux tmp]# awk 'BEGIN{print 1/1.5}'
0.666667
[root@bobilinux tmp]# awk 'BEGIN{print 1*1.5}'
1.5
[root@bobilinux tmp]# awk 'BEGIN{print 2**3}'
8
[root@bobilinux tmp]# awk 'BEGIN{print 2**1.5}'
2.82843
[root@bobilinux tmp]# awk 'BEGIN{print 2%1.5}'
0.5

awk统计案例

1. /etc/passwd shell
[root@bobilinux tmp]# awk -F: '{shells[$NF]++};END{for (i in shells) {print i,shells[i]}}' /etc/passwd
/sbin/shutdown 1
/bin/bash 25
/sbin/nologin 24
/sbin/halt 1
/bin/sync 1



2. 访 < netstat>
# ss -an|grep 80|awk '{states[$1]++};END{for (i in states) {print i,states[i]}}'
TIME_WAIT 578
ESTABLISHED 1
LISTEN 1

# ss -an|grep 80|awk '{states[$1]++};END{for (i in states) {print i,states[i]}}'|sort -k2 -rn
TIME-WAIT 18
ESTAB 8
LISTEN 1



3. 访IP < netstat,ss>
# netstat -ant |grep :80 |awk -F: '{ip_count[$8]++};END{for (i in ip_count) {print i,ip_count[i]}}'|sort



4. Apache/NginxPV  <>
# grep '27/Jul/2017' mysqladmin.cc-access_log |wc -l
14519



5. Apache/NginxIP访 <>
# grep '27/Jul/2017' mysqladmin.cc-access_log|awk '{ips[$1]++};END{for (i in ips) {print i,ips[i]}}' |sort -k2 -rn |head





VV = Visit View访
访1访访30访访

访UV
UV= Unique Visitor访
1访访1UV

PV
PV=PageView ()
访便1PV

IPIP
IP=IP
1使IP访IP访IP1

shell编程之文本处理工具awk_awk+循环控制