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\t%.2f\t%.2f\n",$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\n",$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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM