linux脚本Shell之awk详解(二)

三.printf的使用

 

print format 生成报表

%d        十进制有符号整数  

%u        十进制无符号整数  

%f        浮点数  

%s        字符串  

%c        显示字符的ASCII码  

%p        指针的值  

%e        科学技术法显示数值  

%x        %X 无符号以十六进制表示的整数  

%o        无符号以八进制表示的整数  

%g        %G 以科学计数法或浮点数的格式显示数值  

%%        显示其自身  

修饰符:  

-:  左对齐     

+:  显示数值符号  

N: 显示

-F 指定段的分隔符

例:(1)生成报表

例:(2)小数问题

对小数取保留位的时候,四舍五入

对小数取整,不进行四舍五入

1 [root@tx3 ~]# cat awk.1
2 
3 23.3456 11.234 45.67
4 
5 [root@tx3 ~]# awk '{printf "%.2f	%.2f	%.2f
",$1,$2,$3}' awk.1
6 
7 23.3511.2345.67

四.awk的使用

(1)正则表达式

()   {} 不支持

. * ^ $ ? + [] | < > ()  可以直接使用

 1 [root@tx3 ~]# awk '/^$/{print "this is an empty line"}' /etc/inittab
 2 
 3 this is an empty line
 4 
 5 this is an empty line
 6 
 7 this is an empty line
 8 
 9 this is an empty line
10 
11 this is an empty line
12 
13 this is an empty line
14 
15 this is an empty line
16 
17 this is an empty line
18 
19 this is an empty line

 

1 [root@tx3 ~]# awk -F: '/^root/{print $1,$NF}' /etc/passwd
2 
3 root /bin/bash

 

1 [root@tx3 ~]# awk -F: '!/^root/{print $1,$NF}' /etc/passwd|head -3  
2 
3 bin /sbin/nologin
4 
5 daemon /sbin/nologin
6 
7 adm /sbin/nologin

(2)关系运算符

> < == != >= <=

~(匹配) !~(不匹配)

例:

1 [root@tx3 ~]# cp /etc/passwd p1
2 
3 [root@tx3 ~]# awk -F: '$3 == 0 {print $1}' p1
4 
5 Root

例:

1 [root@tx3 ~]# awk -F: '$3 != 0{ print $1}' p1 | head -2
2 
3 bin
4 
5 Daemon

例:

1 [root@tx3 ~]# awk -F: '$3 < 2 {print $1}' p1
2 
3 root
4 
5 bin

(3)逻辑运算符

&& || !

与 或 非

例:

1 [root@tx3 ~]# awk -F: '$3 > 0 && $3 < 10 {print $1, $3}' p1 |head -2
2 
3 bin 1
4 
5 daemon 2

 

例:

 1 [root@tx3 ~]#  awk -F: '$3 > 10 || $3 < 5 {print $1,$3}' p1 |head -6
 2 
 3 root 0
 4 
 5 bin 1
 6 
 7 daemon 2
 8 
 9 adm 3
10 
11 lp 4
12 
13 operator 11

 

(4)算数运算符

+ - * / %(取模(余数)) ^(幂运算)

例:输出名字,总成绩,平均成绩

1 [root@tx3 ~]# cat cj
2 
3 tx 90 86 86
4 
5 tx1 89 78 85
6 
7 tx2 79 80 85   
1 [root@tx3 ~]#  awk '{print $1,$2+$3+$4,($2+$3+$4)/3}' cj
2 
3 tx 262 87.3333
4 
5 tx1 252 84
6 
7 tx2 244 81.3333
1 [root@tx3 ~]# awk '{printf"%-5s %3d %.2f
",$1,$2+$3+$4,($2+$3+$4)/3}' cj
2 
3 tx    262 87.33
4 
5 tx1   252 84.00
6 
7 tx2   244 81.33

(5)BEGIN  END

BEGIN{ 动作;动作;... }  在处理文件之前,要执行的动作;只执行一次

END{ 动作;动作;... }    在处理完文件之后,要执行的动作;只执行一次

BEGIN :可以给文件添加标题、定义变量、定义文件的分隔符

END:汇总的操作

getline可以从管道和标准输入读取输入,然后传递给变量。

例:

1 [root@tx3 ~]# awk 'BEGIN{"date"| getline a}{print}END{print a}' cj
2 
3 tx 90 86 86
4 
5 tx1 89 78 85
6 
7 tx2 79 80 85  
8 
9 Thu Feb  7 12:39:25 CST 2013

五.awk里的流控制和循环

(1)简单的条件判断

语法:(表达式 ? 值1 : 值2) 如果表达式成立,输出值1;否则输出值2

 1 [root@tx3 ~]# cat num
 2 
 3 2 8 9
 4 
 5 8 4 6
 6 
 7 3 5 7
 8 
 9 [root@tx3 ~]# awk '{print ( $1 > $2 ? $1 : $2)}' num
10 
11 8
12 
13 8
14 
15 5

(2)if判断

语法:

{ if (表达式

{

                动作1;动作2;...

}

}

   如果表达式成立,那么执行动作。

1 [root@tx3 ~]# awk '{if ($2>=80 && $2 <=100) {print $1,"great"} else {print $1, "good"}}' cj
2 
3 tx great
4 
5 tx1 great
6 
7 tx2 good

(2)多支判断

{

if (表达式)

{ 动作1;动作2;...}

else if (表达式)

{ 动作1;动作2;...}

else if (表达式)

{ 动作1;动作2;...}

......

else

{ 动作1;动作2;...}

}

 1 [root@tx3 ~]# cat cj
 2 
 3 tx 90 86 86
 4 
 5 tx1 89 78 85
 6 
 7 tx2 79 80 85  
 8 
 9 tx3 80 70 60
10 
11 tx4 75 85 65
12 
13 tx5 78 62 80

 判断的标准:

90-100 A

80-89  B

70-79  C

60-69  D

0-59   E

 1 [root@tx3 ~]# awk '{ if ($2 >= 90 && $2 <= 100) {print $1,"A"} else if ($2 >= 80 && $2 < 90) {print $1,"B"} else if ($2 >= 70 && $2 < 80) {print $1,"C"} else if ($2 >= 60 && $2 < 70) {print $1,"D"} else {print $1,"E"} }' cj
 2 
 3 tx A
 4 
 5 tx1 B
 6 
 7 tx2 C
 8 
 9 tx3 B
10 
11 tx4 C
12 
13 tx5 C

(3)循环while

语法:'var=初值;while (表达式){动作1;...更新变量的动作;}'

例:

 1 [root@tx3 ~]# awk -F: '{i=1; while (i<=NF) {print $i;i++}}' p1 | head -7
 2 
 3 root
 4 
 5 x
 6 
 7 0
 8 
 9 0
10 
11 root
12 
13 /root
14 
15 /bin/bash
16 
17  

例. 方法一

1 [root@tx3 ~]# awk -F: '{i=NF; while (i>=2) {printf $i ":";i--};print $1}' p1
2 
3 /bin/bash:/root:root:0:0:x:root
4 
5 /sbin/nologin:/bin:bin:1:1:x:bin
6 
7 /sbin/nologin:/sbin:daemon:2:2:x:daemon
8 
9 /sbin/nologin:/var/adm:adm:4:3:x:adm

例. 方法二

1 [root@tx3 ~]# awk 'BEGIN { FS=":" } { i=NF; while (i>=2) {printf $i ":";i--};print $1}' p1
2 
3 /bin/bash:/root:root:0:0:x:root
4 
5 /sbin/nologin:/bin:bin:1:1:x:bin
6 
7 /sbin/nologin:/sbin:daemon:2:2:x:daemon

(4)for循环

语法:

{

for(表达式)

{动作1;...}

}

表达式:分为3部分:

(1)初始化表达式 i=1

(2)测试表达式   i<10

(3)更新测试表达式 i++

语句:

next 处理输入行的下一个输入行

exit 退出

continue 结束本次循环

break 跳出循环

例:

1 [root@tx3 ~]# awk 'BEGIN {FS=":"} {for(i=NF;i>=2;i--) {printf $i ";"};print $1}' p1
2 
3 /bin/bash;/root;root;0;0;x;root
4 
5 /sbin/nologin;/bin;bin;1;1;x;bin
6 
7 /sbin/nologin;/sbin;daemon;2;2;x;daemon
8 
9 /sbin/nologin;/var/adm;adm;4;3;x;adm

 1 [root@tx3 ~]# cat num
 2 
 3 2 8 9
 4 
 5 8 4 6
 6 
 7 3 5 7
 8 
 9 [root@tx3 ~]# awk '{ max=0; i=1; while (i<=NF) { if (max<$i) {max=$i} i++} print max}' num
10 
11 9
12 
13 8
14 
15 7

(5)awk数组

例   使用变量作为数组下标

另外一种读取方式(这种是无序的,j是变量,a是数组)

数组有序

(6)函数

@1split 切割字符串

split("等待被切割的字符串",数组名,"切割用的分隔符")

[root@tx3 ~]# awk 'BEGIN{split("2012/08/23",da,"/");print da[2],da[3],da[1]}'

08 23 2012

@2toupper() 小写转大写

tolower() 大写转小写

1 [root@tx3 ~]# awk '{print toupper($0)}' p1 |head -3
2 
3 ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
4 
5 BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
6 
7 DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN

@3sub()  局部替换

gsub() 全局替换

sub(/要替换的内容/,"替换成什么内容")

gsub(/要替换的内容/,"替换成什么内容")

gsub(/要替换的内容/,"替换成什么内容",指定字段如$7)

例:

1 [root@tx3 ~]# awk -F: '{sub(/root/,"r00t");print}' p1
2 
3 r00t:x:0:0:root:/root:/bin/bash

例:

1 [root@tx3 ~]# awk -F: '{gsub(/root/,"r00t");print}' p1
2 
3 r00t:x:0:0:r00t:/r00t:/bin/bash
4 
5 operator:x:11:0:operator:/r00t:/sbin/nologin

例:

1 [root@tx3 ~]# awk -F[:/] '{gsub(/root/,"r00t",$7);print}' p1
2 
3 root x 0 0 root  r00t  bin bash
4 
5 operator x 11 0 operator  r00t  sbin nologin

@4.length() 计算字符串的长度

1 [root@tx3 ~]# awk -F: '{print length($1),$1}' p1
2 
3 4 root
4 
5 3 bin
6 
7 6 daemon
8 
9 3 adm

@5. 数学计算

 1 [root@tx3 ~]# awk 'BEGIN{print sin(30)}'
 2 
 3 -0.988032
 4 
 5 [root@tx3 ~]# awk 'BEGIN{print cos(60)}'
 6 
 7 -0.952413
 8 
 9 [root@tx3 ~]# awk 'BEGIN{print int(22/6)}'
10 
11 3
12 
13 [root@tx3 ~]# awk 'BEGIN{print sqrt(3)}'
14 
15 1.73205
原文地址:https://www.cnblogs.com/itxiongwei/p/5546000.html