awk基本命令
1.awk '{print $1}' /etc/fstab
默認以空格爲分割符的第一個字段打印出來,空格不算
2.awk '{print $1 "\t " $3}' /etc/fstab
打印兩個字段,
lu@lu-VirtualBox:~$ awk '{print $1 "\t " $3}' /etc/fstab
# static
#
# 'blkid'
# this
# works
#
# system>
# was
UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf ext4
# was
對比
lu@lu-VirtualBox:~$ awk '{print $1 "\t " $2}' /etc/fstab
# /etc/fstab:
#
# Use
# device;
# that
#
# <file
# /
UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf /
# swap
UUID=990ee444-dada-441f-9fff-ca2efffaeb4e none
完整文檔:
lu@lu-VirtualBox:~$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf / ext4 errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=990ee444-dada-441f-9fff-ca2efffaeb4e none swap sw 0 0
將字段說明加到輸出中:
lu@lu-VirtualBox:~$ awk '{print "device:" $2 "\t " "fstype:" $3}' /etc/fstab
device:/etc/fstab: fstype:static
device: fstype:
device:Use fstype:'blkid'
device:device; fstype:this
device:that fstype:works
device: fstype:
device:<file fstype:system>
device:/ fstype:was
device:/ fstype:ext4
device:swap fstype:was
device:none fstype:swap
lu@lu-VirtualBox:~$ cat /etc/passwd | head -n10
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
在臨時文件中操作:
lu@lu-VirtualBox:~$ head -n10 /etc/passwd >/tmp/passwd.piece
lu@lu-VirtualBox:~$ cat /tmp/passwd.piece
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
#-F設置分隔符,默認空白字符
lu@lu-VirtualBox:~$ awk -F ":" '{print "user: "$1 "\tSHELL: "$7}' /tmp/passwd.piece
user: root SHELL: /bin/bash
user: daemon SHELL: /usr/sbin/nologin
user: bin SHELL: /usr/sbin/nologin
user: sys SHELL: /usr/sbin/nologin
user: sync SHELL: /bin/sync
user: games SHELL: /usr/sbin/nologin
user: man SHELL: /usr/sbin/nologin
user: lp SHELL: /usr/sbin/nologin
user: mail SHELL: /usr/sbin/nologin
user: news SHELL: /usr/sbin/nologin
#設置多個分隔符
lu@lu-VirtualBox:~$ awk -F "[\t ]+" '{print "device: "$1 "\tFSTYPE: "$3}' /etc/fstab
device: # FSTYPE: static
device: # FSTYPE:
device: # FSTYPE: 'blkid'
device: # FSTYPE: this
device: # FSTYPE: works
device: # FSTYPE:
device: # FSTYPE: system>
device: # FSTYPE: was
device: UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf FSTYPE: ext4
device: # FSTYPE: was
device: UUID=990ee444-dada-441f-9fff-ca2efffaeb4e FSTYPE: swap
#將頭10行輸入管道再用AWK命令格式輸出每行的第1個,第7個字符
lu@lu-VirtualBox:~$ head -n10 /etc/passwd | awk -F":" '{print "User:" $1 "\tSHELL:"$7}'
User:root SHELL:/bin/bash
User:daemon SHELL:/usr/sbin/nologin
User:bin SHELL:/usr/sbin/nologin
User:sys SHELL:/usr/sbin/nologin
User:sync SHELL:/bin/sync
User:games SHELL:/usr/sbin/nologin
User:man SHELL:/usr/sbin/nologin
User:lp SHELL:/usr/sbin/nologin
User:mail SHELL:/usr/sbin/nologin
User:news SHELL:/usr/sbin/nologin
#SHELL操作
#BEGIN,END代碼塊
#-f指定awk文件傳遞到命令中
lu@lu-VirtualBox:~$ cat fs.awk
cat: fs.awk: 沒有那個文件或目錄
lu@lu-VirtualBox:~$ vi fs.awk
lu@lu-VirtualBox:~$ cat fs.awk
BEGIN{
FS=":"
}
{
print "user: "$1 "\tshell:"$7
}
#將冒號“:”賦值給FS變量,命令將默認以:爲分隔符
lu@lu-VirtualBox:~$ head -n10 /etc/passwd| awk -f fs.awk
user: root shell:/bin/bash
user: daemon shell:/usr/sbin/nologin
user: bin shell:/usr/sbin/nologin
user: sys shell:/usr/sbin/nologin
user: sync shell:/bin/sync
user: games shell:/usr/sbin/nologin
user: man shell:/usr/sbin/nologin
user: lp shell:/usr/sbin/nologin
user: mail shell:/usr/sbin/nologin
user: news shell:/usr/sbin/nologin
#END塊一般用於統計數據、打印輸出之類的操作
lu@lu-VirtualBox:~$ vi search.awk
BEGIN{
print "how many people with nologin?"
}
/nologin/{++adder}#正則表達式
#匹配成功則執行{}之前的內容,adder變量計數+1
END{
print "'nologin' apperars " adder " times"
}
lu@lu-VirtualBox:~$ vi search.awk
lu@lu-VirtualBox:~$ awk -f search.awk /etc/passwd
how many people with nologin?
'nologin' apperars 17 times
#此處有坑
lu@lu-VirtualBox:~$ awk '/^\r/{print "this is an empty line."} ' /test/First.java
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
lu@lu-VirtualBox:~$ awk '/^$/{print "this is an empty line."} ' /test/test.txt
this is an empty line.
this is an empty line.
this is an empty line.
#查找有OUT的行
lu@lu-VirtualBox:~$ awk '/out/{print "this is an empty line."} ' /test/First.java
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
#查找使用BASH作爲登錄SHELL的用戶名
lu@lu-VirtualBox:~$ awk -F":" '/bash/{print $1}' /etc/passwd
root
lu
lu@lu-VirtualBox:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
lu:x:1000:1000:lu,,,:/home/lu:/bin/bash
AWK 數組
AWK 可以使用關聯數組這種數據結構,索引可以是數字或字符串。
AWK關聯數 組也不需要提前聲明其大小,因爲它在運行時可以自動的增大或減小。
數組使用的語法格式:
array_name[index]=value
array_name:數組的名稱
index:數組索引
value:數組中元素所賦予的值
創建數組
接下來看一下如何創建數組以及如何訪問數組元素:
lu@lu-VirtualBox:~$ awk 'BEGIN {sites["hello"]="world"; print sites["hello"]}'
world
lu@lu-VirtualBox:~$ awk 'BEGIN {
> sites["hello"]="world";
> sites["google"]="www.google.com";
> print sites["hello"] "\n" sites["google"]
> }'
world
www.google.com
也可以使用如下格式訪問數組元素:
array_name[index]
刪除數組元素
我們可以使用 delete 語句來刪除數組元素,語法格式如下:
delete array_name[index]
下面的例子中,數組中的 google 元素被刪除(刪除命令沒有輸出):
lu@lu-VirtualBox:~$ awk 'BEGIN {
sites[0]="world";
sites[1]="www.google.com"
delete sites[0];
print sites[0] "\n" sites[1]
}'
www.google.com
lu@lu-VirtualBox:~$ awk 'BEGIN {
sites[0]="world";
sites[1]="www.google.com"
#delete sites[0];
print sites[0] "\n" sites[1]
}'
world
www.google.com
lu@lu-VirtualBox:~$ awk 'BEGIN {
sites["hello"]="world";
sites["google"]="www.google.com"
delete sites["google"];
print sites["hello"] "\n" sites["google"]
}'
world
#多維數組
AWK 本身不支持多維數組,不過我們可以很容易地使用一維數組模擬實現多維數組。
如下示例爲一個 3x3 的三維數組:
100 200 300
400 500 600
700 800 900
以上實例中,array[0][0] 存儲 100,array[0][1] 存儲 200 ,依次類推。爲了在 array[0][0] 處存儲 100, 我們可以使用如下語法: array["0,0"] = 100。
我們使用了 0,0 作爲索引,但是這並不是兩個索引值。事實上,它是一個字符串索引 0,0。
下面是模擬二維數組的例子:
lu@lu-VirtualBox:~$ awk 'BEGIN {
> array["0,0"] = 100;
> array["0,1"] = 200;
> array["0,2"] = 300;
> array["1,0"] = 400;
> array["1,1"] = 500;
> array["1,2"] = 600;
> # 輸出數組元素
> print "array[0,0] = " array["0,0"];
> print "array[0,1] = " array["0,1"];
> print "array[0,2] = " array["0,2"];
> print "array[1,0] = " array["1,0"];
> print "array[1,1] = " array["1,1"];
> print "array[1,2] = " array["1,2"];
> }'
array[0,0] = 100
array[0,1] = 200
array[0,2] = 300
array[1,0] = 400
array[1,1] = 500
array[1,2] = 600
在數組上可以執行很多操作,比如,使用 asort 完成數組元素的排序,或者使用 asorti 實現數組索引的排序等等。
判斷閏年
lu@lu-VirtualBox:~$ vi leap.awk
BEGIN{
print "pick leap years:"
}
{
year =$1
if((year %4 ==0 &&year % 100!=0) || year % 400==0)
print year "is leap year"
else
print year " is not leap"
}
lu@lu-VirtualBox:~$ awk -f leap.awk year.txt
pick leap years:
1987 is not leap
2008is leap year
3000 is not leap
2000is leap year
2012is leap year
1200is leap year
1300 is not leap