awk 用法
阿新 • • 發佈:2018-11-25
[[email protected] ~]# # awk 引數 '模式{動作}' 檔案
[[email protected] ~]# # awk 引數 '條件(找誰) {幹啥}' 檔案
[[email protected] ~]# awk -F ":" 'NR==2{print $2,$4}' /etc/passwd
x 1
[[email protected] ~]# cat /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
[ [email protected] ~]# mkdir -p /server/files
[[email protected] ~]# cd /server/files/
[[email protected] files]# cat >>files<<EOF
> Zhang Dandan 41116397 :250:100:175
> Zhang xiaoyu 390320151:155:90:201
> Meng Feifei 80042789 :250:60:50
> wu Waiwai 70271111 :250:100:175
> wang xiaoai 3515064655 :50:95:135
> Zi Gege 1986786350 :250:168:200
> Li Youjiu 918391635 :175:75:300
> Lao Nanhai 918391685 :250:100:175
> EOF
[ [email protected] files]# ls
files
[[email protected] files]# cat files
Zhang Dandan 41116397 :250:100:175
Zhang xiaoyu 390320151:155:90:201
Meng Feifei 80042789 :250:60:50
wu Waiwai 70271111 :250:100:175
wang xiaoai 3515064655 :50:95:135
Zi Gege 1986786350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391685 :250:100:175
[ [email protected] files]# awk '/1/' files
Zhang Dandan 41116397 :250:100:175
Zhang xiaoyu 390320151:155:90:201
wu Waiwai 70271111 :250:100:175
wang xiaoai 3515064655 :50:95:135
Zi Gege 1986786350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391685 :250:100:175
[[email protected] files]# #顯示xiaoyu的姓氏和ID號碼
[[email protected] files]# awk '/xiaoyu/{print$1,$2, $3}' files
Zhang xiaoyu 390320151:155:90:201
[[email protected] files]# awk '$2~/xiaoyu/' files (~號表示匹配xiaoyu的行)
Zhang xiaoyu 390320151:155:90:201
awk '$2~/xiaoyu/{print $1,$2,$3}' files (表示第二列中包含xiaoyu的行,精確到某一列的內容)
Zhang xiaoyu 390320151
[[email protected] files]# awk '$0~/xiaoyu/' files ($0 表示這一整行)
Zhang xiaoyu 390320151 :155:90:201
[[email protected] files]# awk '/xiaoyu/' files
Zhang xiaoyu 390320151 :155:90:201
[[email protected] files]# awk '$3 ~/^41/' files
Zhang Dandan 41116397 :250:100:175
Meng Feifei 4180042789 :250:60:50
[[email protected] files]# awk '$3 ~/^41/' files (第三列以41開頭的行)
Zhang Dandan 41116397 :250:100:175
Meng Feifei 4180042789 :250:60:50
# #顯示所有已41開頭的行的ID號碼的人的全名和ID號碼
[[email protected] files]# awk '$3 ~/^41/{print $1,$2,$3}' files
Zhang Dandan 41116397
Meng Feifei 4180042789
## 顯示第三例結尾是1或5行的人的全名和ID號碼
[[email protected] files]# awk '$3~/[1,5]$/' files
Zhang xiaoyu 390320151 :155:90:201
wu Waiwai 70271111 :250:100:175
wang xiaoai 3515064655 :50:95:135
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391685 :250:100:175
[[email protected] files]# awk '$3~/[1,5]$/{print $1,$2}' files
Zhang xiaoyu
wu Waiwai
wang xiaoai
Li Youjiu
Lao Nanhai
顯示第三例結尾是1或5行的人的全名和ID號碼
[[email protected] files]# awk '$3~/[1,5]$/{print $1,$2,$3}' files
Zhang xiaoyu 390320151
wu Waiwai 70271111
wang xiaoai 3515064655
Li Youjiu 918391635
Lao Nanhai 918391685
:冒號替換成$符號
[[email protected] files]# sed 's#:#$#g' files
Zhang Dandan 41116397 $250$100$175
Zhang xiaoyu 390320151 $155$90$201
Meng Feifei 4180042789 $250$60$50
wu Waiwai 70271111 $250$100$175
wang xiaoai 3515064655 $50$95$135
Zi Gege 1986786350 $250$168$200
Li Youjiu 918391635 $175$75$300
Lao Nanhai 918391685 $250$100$17
awk '{gsub(/匹配/,"替換成什麼",$4);print}' 固定格式
[[email protected] files]# awk '{gsub(/:/,"$",$4);print}' files
Zhang Dandan 41116397 $250$100$175
Zhang xiaoyu 390320151 $155$90$201
Meng Feifei 4180042789 $250$60$50
wu Waiwai 70271111 $250$100$175
wang xiaoai 3515064655 $50$95$135
Zi Gege 1986786350 $250$168$200
Li Youjiu 918391635 $175$75$300
Lao Nanhai 918391685 $250$100$175
匹配第二列的/xiaoyu/,並將:替換為$
[[email protected] files]# awk '$2~/xiaoyu/{gsub(/:/,"$");print}' files (gsub 類似於sed的功能 gsub 表示替換的意思)
Zhang xiaoyu 390320151 $155$90$201
[[email protected] files]# awk '/^$/{print NR}' /etc/services
22
266
299
320
326
[[email protected] files]# awk '/^$/{i=i+1;print i}' /etc/services (統計出現的次數)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[[email protected] files]# awk '/^$/{i=i+1}END{print i}' /etc/services (統計總共有多少行,END表示在awk 執行完畢後再進行計算總共有多少行)先計算後統計
16