AWK的格式化輸出和檔案中的AWK

Augusdi發表於2015-06-19


一,Output FroMaT(輸出格式)
1,
OFMT變數,在OFMT中定義數字的格式;預設為“%.6gd”,只會列印小數點後6位。
2,
[root@rhel helinbash]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2              9920624   6479488   2929068  69% /
/dev/sda5             26742436   6534784  18827272  26% /u01
/dev/sda1               988088     23460    913624   3% /boot
tmpfs                   517568         0    517568   0% /dev/shm
[root@rhel helinbash]# df | grep -v 'Available'| gawk '$4 >200000 { OFMT="%.2f"; print $1,$4/1024,"M"}'
/dev/sda2 2860.42 M
/dev/sda5 18386.01 M
/dev/sda1 892.21 M
tmpfs 505.44 M
[root@rhel helinbash]#
[root@rhel helinbash]# df | grep -v 'Available'| gawk '$4 >2000000 { OFMT="%.2f"; print $1,$4/1024,"M"}'
/dev/sda2 2860.42 M
/dev/sda5 18386.01 M
[root@rhel helinbash]#
3,
[root@rhel helinbash]# expr 10/5
10/5
[root@rhel helinbash]# expr 10 / 5
2
[root@rhel helinbash]# expr 10 / 3
3
[root@rhel helinbash]# expr 11 / 3
3
[root@rhel helinbash]#
4,
[root@rhel helinbash]# echo -e "1000 100\n2000 101"
1000 100
2000 101
[root@rhel helinbash]# echo -e "1000 100\n2000 101" | gawk '$2 >100'
2000 101
[root@rhel helinbash]# echo -e "1000 100\n2000 101" | gawk '$2 >100 {print $1 /$2 }'
19.802
[root@rhel helinbash]# echo -e "1000 100\n2000 101" | gawk '$2 >100 { OFMR="%.2f"; print $1 /$2 }'  
19.802
[root@rhel helinbash]# echo -e "1000 100\n2000 101" | gawk '$2 >100 { OFMT="%.2f"; print $1 /$2 }'
19.80
[root@rhel helinbash]# echo -e "1000 100\n2000 97" | gawk '$2 <100 { OFMT="%.2f"; print $1 /$2 }'     
20.62
[root@rhel helinbash]# echo -e "1000 100\n2000 97" | gawk '$2 <100 { OFMT="%.4f"; print $1 /$2 }'
20.6186
[root@rhel helinbash]#
5,
printf函式轉義字元;printf與C語言中的printf雷同;
(1)
轉義字元
%c 字元;
printf("The character is %c\n",x);
%s 字串
%d 十進位制整數
printf("The boy is %d years old\n,50")
%f 浮點數
(2)
[root@rhel helinbash]# gawk '{printf("===");}' names.txt
==================[root@rhel helinbash]# cat names.txt


Tom Savage                 100
Molly   Lee           200
John  Doe       300

[root@rhel helinbash]#
[root@rhel helinbash]#



[root@rhel helinbash]# gawk '{printf("===");}' names.txt
==================[root@rhel helinbash]# cat names.txt


Tom Savage                 100
Molly   Lee           200
John  Doe       300

[root@rhel helinbash]#
[root@rhel helinbash]# gawk '{printf("===\n\n");}' names.txt
===

===

===

===

===

===

[root@rhel helinbash]# gawk '{printf("===\n\n",$1);}' names.txt
===

===

===

===

===

===

[root@rhel helinbash]#
[root@rhel helinbash]# gawk '{printf("%s===%d\n\n",$1,$3);}' names.txt  
===0

===0

Tom===100

Molly===200

John===300

===0

[root@rhel helinbash]# gawk '{printf("%s\t%s\t%d\n",$1,$2,$3);}' names.txt
                0
                0
Tom     Savage  100
Molly   Lee     200
John    Doe     300
                0
[root@rhel helinbash]#
6,
printf函式修飾符;
列印時需要對齊,下面提供一些列印輸出時所用到的修飾符;
-(橫槓) 左對齊;
(1)
[root@rhel helinbash]# echo "Bluefox" | gawk '{ printf "|%-15s|\n",$1}'
|Bluefox        |
[root@rhel helinbash]# echo "Bluefox" | gawk '{ printf "|%15s|\n",$1}'
|        Bluefox|
[root@rhel helinbash]#
(2)
#(井號)顯示8進位制時前面加0,顯示16進位制時加0X
+(加好)顯示正負值時正+負-號
0(零)用0對顯示值填充空白處
(3)
[root@rhel helinbash]# echo "Bluefox" | gawk '{ printf (" %s \n",$0)}'
 Bluefox
[root@rhel helinbash]# echo "Bluefox" | gawk '{ printf ("| %s | \n",$0)}'
| Bluefox |
[root@rhel helinbash]#
(4)
[root@rhel helinbash]# gawk '{printf("?\t?\t?\n",$1,$2,$3)}' names.txt
?       ?       ?
?       ?       ?
?       ?       ?
?       ?       ?
?       ?       ?
?       ?       ?
[root@rhel helinbash]# gawk '{printf("%20s\t%-20s\t%-15d\n",$1,$2,$3)}' names.txt
                 Tom    Savage                  100            
               Molly    Lee                     200            
                John    Doe                     300            
                yang    wawa                    -121212        
[root@rhel helinbash]# gawk '{printf("%20s\t%-20s\t%+-15d\n",$1,$2,$3)}' names.txt
                 Tom    Savage                  +100           
               Molly    Lee                     +200           
                John    Doe                     +300           
                yang    wawa                    -121212        
[root@rhel helinbash]#
(5)
[root@rhel helinbash]# gawk 'BEGIN{printf("%d",0x1F)}'
31[root@rhel helinbash]# gawk 'BEGIN{printf("%d\n",0x1F)}'
31
[root@rhel helinbash]# gawk 'BEGIN{printf("%d\n",0x20)}'
32
[root@rhel helinbash]#
[root@rhel helinbash]# gawk 'BEGIN{printf("%d\n",0xFF)}'
255
[root@rhel helinbash]#
二,檔案中的awk命令
1,
將awk寫入一個檔案中,更適合複雜的程式
使用-f選項指定awk的檔名;
awk一次讀取一條記錄,測試檔案中的每一條命令這樣迴圈;
2,
(1)

[root@rhel helinbash]# gawk '{printf("%s - %s - %s\n",$1,$2,$3)}' names.txt
Tom - Savage - 100
Molly - Lee - 200
John - Doe - 300
yang - wawa - -121212
[root@rhel helinbash]# gawk '/^Molly/{printf("%s - %s - %s\n",$1,$2,$3)}' names.txt  
Molly - Lee - 200
[root@rhel helinbash]#

[root@rhel helinbash]# gawk '/^Molly/{printf("Welcome %s",$1)}' names.txt                     
Welcome Molly[root@rhel helinbash]#
(2)
[root@rhel helinbash]# cat names.txt
Tom Savage                 100
Molly   Lee           200
John  Doe       300
yang wawa -121212
[root@rhel helinbash]# vim my.awk              
[root@rhel helinbash]# gawk -f my.awk names.txt
Tom - Savage - 100
Molly - Lee - 200
Nice to meet you -> Molly
John - Doe - 300
yang - wawa - -121212
[root@rhel helinbash]# cat my.awk
#!/bin/gawk


{printf("%s - %s - %d\n",$1,$2,$3)}

/^Mo/ { printf("Nice to meet you -> %s\n",$1) }
[root@rhel helinbash]#
(3)
[root@rhel helinbash]# vim my.awk              
[root@rhel helinbash]# gawk -f my.awk names.txt
Tom - Savage - 100
Nice to meet you -> Tom
Molly - Lee - 200
Molly   Lee           200
Nice to meet you -> Molly
John - Doe - 300
Nice to meet you -> John
yang - wawa - -121212
Nice to meet you -> yang
[root@rhel helinbash]# cat my.awk
#!/bin/gawk
(4)
{printf("%s - %s - %d\n",$1,$2,$3)} #這個規則也是無條件列印
/^Mo/   #這個僅僅變成了過濾規則
 { printf("Nice to meet you -> %s\n",$1) }   #沒有任何條件就會列印的,預設一行是一條規則。
[root@rhel helinbash]#
逐條按照指令碼里面的規則(這裡有3個規則)進行過濾和並列印。
(5)
[root@rhel helinbash]# vim my.awk
[root@rhel helinbash]# gawk -f my.awk names.txt
Tom - Savage - 100
Molly - Lee - 200
Nice to meet you -> Molly
John - Doe - 300
yang - wawa - -121212
[root@rhel helinbash]# cat my.awk              
#!/bin/gawk


{printf("%s - %s - %d\n",$1,$2,$3)}

/^Mo/ {
printf("Nice to meet you -> %s\n",$1)
 }#這個就變得正常了!!!!!
[root@rhel helinbash]#
(6)
[root@rhel helinbash]# gawk -f my.awk names.txt
Tom - Savage - 100
Molly - Lee - 200
Nice to meet you -> Molly
John - Doe - 300
yang - wawa - -121212
[root@rhel helinbash]# cat my.awk              
#!/bin/gawk


{printf("%s - %s - %d\n",$1,$2,$3)}

/^Mo/    \
{
printf("Nice to meet you -> %s\n",$1)
 
}
[root@rhel helinbash]#


http://blog.itpub.net/29611940/viewspace-1172651/

相關文章