awk學習筆記
文章目錄
- awk語法格式
- awk命令格式
- awk 'pattern' filename
- awk '{action}' filename
- awk 'pattern {action}' filename
- command |awk 'pattern {action}'
- awk工作原理
- awk 中與記錄字段相關的內部變量:
- 1. **$0**: awk變量$0保存當前記錄的內容
- 2. **NR**: The **total** number of input records seen so far.
- 3. **FNR**: The input record number in the current input file
- 4. **NF**:保存記錄的字段數,$1 , $2... $100
- 5. **FS**: 輸入字段分隔符,默認空格
- 6. **OFS**: 輸出字段分隔符
- 7. **RS** : The input record separator, by default a newline.
- 8. **ORS** : The output record separator, by default a newline.
- 格式化輸出:
- awk模式和動作
- awk支持的模式
- awk示例:
- lab3
awk語法格式
awk [options] ‘commands’ filenames
options
-F 定義輸入字段分隔符,默認的分隔符是空格或製表符(tab)
command:
BEGIN{}{}END{}
行處理前 行處理 行處理後
# awk 'BEGIN{print 1/2} {print "ok"} END{print "-----------"}' /etc/hosts
0.5
ok
ok
ok
-----------
BEGIN{} 通常用於定義一些變量,
例如 BEGIN{FS=":";OFS="—"}
awk命令格式
awk ‘pattern’ filename
示例:
awk -F: '/root/' /etc/passwd
awk ‘{action}’ filename
示例:
awk -F: '{print $1}' /etc/passwd
awk ‘pattern {action}’ filename
示例:
awk -F: '/root/{print $1,$3}' /etc/passwd
示例
awk 'BEGIN{FS=":"} /root/{print $1,$3}' /etc/passwd
command |awk ‘pattern {action}’
示例:
df -P| grep '/' |awk '$4 > 25000 {print $4}'
awk工作原理
# awk -F: '{print $1,$3}' /etc/passwd
- awk 使用一行作爲輸入,並將這一行賦給內部變量$0,每一行也可稱爲一個記錄,以換行符結束
- 然後,行被:(默認爲空格或製表符)分解成字段(或域),每個字段存儲在已編號的變量
中,從 $1 開始, 最多達100個字段 - awk 如何知道用空格來分隔字段的呢? 因爲有一個內部變量 FS 來確定字段分隔符。初始
時,FS 賦爲空格 - awk 打印字段時,將以設置的方法使用print函數打印,awk在打印的字段間加上空格,
因爲$1,$3之間有一個逗號。
逗號比較特殊,它映射爲另一個內部變量,稱爲輸出字段分隔符OFS,OFS默認爲空格
- awk 輸出之後,將從文件中獲取另一行,並將其存儲在$0中,覆蓋原來的內容,然後將新的字符串分隔成字段並進行處理。該過程將持續到所有行處理完畢
awk 中與記錄字段相關的內部變量:
查看awk變量方法 man awk
1. $0: awk變量$0保存當前記錄的內容
```
# awk -F: '{print $0}' /etc/passwd
```
2. NR: The total number of input records seen so far.
```
# awk -F: '{print NR, $0}' /etc/passwd /etc/hosts
```
3. FNR: The input record number in the current input file
```
# awk -F: '{print FNR, $0}' /etc/passwd
/etc/hosts
```
4. NF:保存記錄的字段數,$1 , $2… $100
```
# awk -F: '{print $0,NF}' /etc/passwd
```
>**注意:** NF 和 $NF 之間的區別
5. FS: 輸入字段分隔符,默認空格
```
# awk -F: '/alice/{print $1, $3}' /etc/passwd
# awk -F'[ :\t]' '{print $1,$2,$3}' /etc/passwd
# awk 'BEGIN{FS=":"} {print $1,$3}' /etc/passwd
```
6. OFS: 輸出字段分隔符
```
# awk -F: '/alice/{print $1,$2,$3,$4}' /etc/passwd
# awk 'BEGIN{FS=":"; OFS="+++"} /^root/{print $1,$2,$3,$4}' /etc/passwd
```
7. RS : The input record separator, by default a newline.
輸入記錄分隔符,默認遇見換行符認爲是一條記錄
# awk -F: 'BEGIN{RS=" "} {print $0}' a.txt
8. ORS : The output record separator, by default a newline.
```
# awk -F: 'BEGIN{ORS=""} {print $0}' passwd
```
lab1: 將文件中 多行 合併爲 一行
# awk 'BEGIN{ORS=" "} {print $0}' /etc/passwd
lab2: 將文件中一行分成多行
# head -1 /etc/passwd > passwd1
# cat passwd1
root:x:0:0:root:/root:/bin/bash
# awk 'BEGIN{RS=":"} {print $0}' passwd1
root
x
0
0
root
/root
/bin/bash
# awk 'BEGIN{RS=":"} {print $0}' passwd1 |grep -v '^$' > passwd2
格式化輸出:
print函數
# date |awk '{print "Month: " $2 "\nYear: " $NF}'
# awk -F: '{print "username is: " $1 "\t uid is: " $3}' /etc/passwd
# awk -F: '{print "\tusername and uid: " $1,$3 "!"}' /etc/passwd
printf函數
# awk -F: '{printf "%-15s %-10s %-15s\n", $1,$2,$3}' /etc/passwd
# awk -F: '{printf "|%-15s| %-10s| %-15s|\n", $1,$2,$3}' /etc/passwd
- %s 字符類型
- %d 數值類型
- %f 浮點數
- 佔15字符
- - 表示左對齊,默認是右對齊
printf默認不會在行尾自動換行,加\n
awk模式和動作
任何awk語句都由模式和動作組成。
模式 部分決定動作語句何時觸發及觸發事件。
動作 即對數據進行的操作。
如果省略模式部分,動作將時刻保持執行狀態。
模式可以是任何條件語句或複合語句或正則表達式。
模式包括兩個特殊字段BEGIN和END。
- 使用BEGIN語句設置計數和打印頭。
BEGIN語句使用在任何文本瀏覽動作之前,之後文本瀏覽動作依據輸入文本開始執行。 - END語句用來在awk完成文本瀏覽動作後打印輸出文本總數和結尾狀態。
awk支持的模式
正則表達式
匹配記錄(整行)
# awk '/^alice/' /etc/passwd
# awk '$0 ~ /^alice/' /etc/passwd
# awk '!/alice/' passwd
# awk '$0 !~ /^alice/' /etc/passwd
匹配字段:匹配操作符(~ !~)
# awk -F: '$1 ~ /^alice/' /etc/passwd
# awk -F: '$NF !~ /bash$/' /etc/passwd
比較表達式:
- 比較表達式採用對文本進行比較,只有當條件爲真,才執行指定的動作。
- 比較表達式使用關係運算符,用於比較數字與字符串。
關係運算符
運算符 | 含義 | 示例 |
---|---|---|
< | 小於 | x<y |
<= | 小於或等於 | x<=y |
== | 等於 | x==y |
!= | 不等於 | x!=y |
>= | 大於等於 | x>=y |
> | 大於 | x>y |
示例:
# awk -F: '$3 == 0' /etc/passwd
# awk -F: '$3 < 10' /etc/passwd
# awk -F: '$7 == "/bin/bash"' /etc/passwd
# awk -F: '$1 == "alice"' /etc/passwd
# awk -F: '$1 ~ /alice/ ' /etc/passwd
# awk -F: '$1 !~ /alice/ ' /etc/passwd
# df -P | grep '/' |awk '$4 > 25000'
條件表達式:
# awk -F: '$3>300 {print $0}' /etc/passwd
# awk -F: '{ if($3>300) print $0 }' /etc/passwd
// 推薦使用以下兩種格式
# awk -F: '{ if($3>300) {print $0} }' /etc/passwd
# awk -F: '{ if($3>300) {print $3} else{print $1} }' /etc/passwd
算術運算:+ - * / %(模) ^(冪2^3)
可以在模式中執行計算,awk都將按浮點數方式執行算術運算
# awk -F: '$3 * 10 > 500' /etc/passwd
# awk -F: '{ if($3*10>500){print $0} }' /etc/passwd
邏輯操作符和複合模式
&& 邏輯與 a&&b
|| 邏輯或 a||b
! 邏輯非 !a
# awk -F: '$1~/root/ && $3<=15' /etc/passwd
# awk -F: '$1~/root/ || $3<=15' /etc/passwd
# awk -F: '!($1~/root/ || $3<=15)' /etc/passwd
範圍模式
# awk '/Tom/,/Suzanne/' filename
awk示例:
# awk '/west/' datafile
# awk '/^north/' datafile
# awk '$3 ~ /^north/' datafile
# awk '/^(no|so)/' datafile
# awk '{print $3,$2}' datafile
# awk '{print $3 $2}' datafile
# awk '{print $0}' datafile
# awk '{print "Number of fields: "NF}' datafile
# awk '/northeast/{print $3,$2}' datafile
# awk '/E/' datafile
# awk '/^[ns]/{print $1}' datafile
# awk '$5 ~ /\.[7-9]+/' datafile
# awk '$2 !~ /E/{print $1,$2}' datafile
# awk '$3 ~ /^Joel/{print $3 " is a nice boy."}' datafile
# awk '$8 ~ /[0-9][0-9]$/{print $8}' datafile
# awk '$4 ~ /Chin$/{print "The price is $" $8 "."}' datafile
# awk '/Tj/{print $0}' datafile
# awk '{print $1}' /etc/passwd
# awk -F: '{print $1}' /etc/passwd
# awk '{print "Number of fields: "NF}' /etc/passwd
# awk -F: '{print "Number of fields: "NF}' /etc/passwd
# awk -F"[ :]" '{print NF}' /etc/passwd
# awk '$7 == 5' datafile
# awk '$2 == "CT" {print $1, $2}' datafile
# awk '$7 != 5' datafile
lab3
# cat b.txt
yang sheng:is a:good boy!
# awk '{print NF}' b.txt
4
# awk -F: '{print NF}' b.txt
3
# awk -F"[ :]" '{print NF}' b.txt
6
# awk '$7 < 5 {print $4, $7}' datafile
# awk '$6 > .9 {print $1,$6}' datafile
# awk '$8 <= 17 {print $8}' datafile
# awk '$8 >= 17 {print $8}' datafile
# awk '$8 > 10 && $8 < 17' datafile
# awk '$2 == "NW" || $1 ~ /south/ {print $1, $2}' datafile
# awk '!($8 == 13){print $8}' datafile
# awk '/southem/{print $5 + 10}' datafile
# awk '/southem/{print $8 + 10}' datafile
# awk '/southem/{print $5 + 10.56}' datafile
# awk '/southem/{print $8 - 10}' datafile
# awk '/southem/{print $8 / 2 }' datafile
# awk '/southem/{print $8 / 3 }' datafile
# awk '/southem/{print $8 * 2 }' datafile
# awk '/southem/{print $8 % 2 }' datafile
# awk '$3 ~ /^Suan/ {print "Percentage: "$6 + .2 " Volume: " $8}' datafile
# awk '/^western/,/^eastern/' datafile
# awk '{print ($7 > 4 ? "high "$7 : "low "$7)}' datafile //三目運算符 a?b:c 條件?結果1:結果2
# awk '$3 == "Chris" {$3 = "Christian"; print $0}' datafile //賦值運算符
# awk '/Derek/ {$8+=12; print $8}' datafile //$8 += 12等價於$8 = $8 + 12
# awk '{$7%=3; print $7}' datafile //$7 %= 3等價於$7 = $7 % 3