FS:輸入字段分隔符,默認爲空白字符 awk -v FS=':' '{print $1,FS,$3}’ /etc/passwd awk –F: '{print $1,$3,$7}’ /etc/passwd
OFS:輸出字段分隔符,默認爲空白字符 awk -v FS=‘:’ -v OFS=‘:’ '{print $1,$3,$7}’ /etc/passwd
ORS:輸出記錄分隔符,輸出時用指定符號代替換行符 awk -v RS=' ' -v ORS='###'‘{print }’ /etc/passwd
NF:字段數量 awk -F:‘{print NF}’ /etc/fstab 引用變量時,變量前不需加$ awk -F:‘{print $(NF-1)}' /etc/passwd
下次引用變量express
![](http://static.javashuo.com/static/loading.gif)
引用第一行shell 變量數組
![](http://static.javashuo.com/static/loading.gif)
取出使用率函數
printf命令
格式化輸出:printf 「FORMAT」, item1, item2, ...
(1) 必須指定FORMAT
(2) 不會自動換行,須要顯式給出換行控制符,\n
(3) FORMAT中須要分別爲後面每一個item指定格式符
格式符:與item一一對應
%c:顯示字符的ASCII碼
%d, %i:顯示十進制整數
%e, %E:顯示科學計數法數值
%f:顯示爲浮點數
%g, %G:以科學計數法或浮點形式顯示數值
%s:顯示字符串
%u:無符號整數
%%:顯示%自身
修飾符
#[.#] 第一個數字控制顯示的寬度;第二個#表示小數點後精度,%3.1f
- 左對齊(默認右對齊) %-15s
+ 顯示數值的正負符號 %+d
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
++i (i=i+1)spa
![](http://static.javashuo.com/static/loading.gif)
哪一個在前先算哪一個3d
操做符
算術操做符:
x+y, x-y, x*y, x/y, x^y, x%y
- x:轉換爲負數
+x:將字符串轉換爲數值
字符串操做符:沒有符號的操做符,字符串鏈接
賦值操做符:
=, +=, -=, *=, /=, %=, ^=,++, --
下面兩語句有何不一樣
• awk ‘BEGIN{i=0;print ++i,i}’
• awk ‘BEGIN{i=0;print i++,i}’操做符
比較操做符:
==, !=, >, >=, <, <=
模式匹配符:
![](http://static.javashuo.com/static/loading.gif)
~:左邊是否和右邊匹配,包含blog
![](http://static.javashuo.com/static/loading.gif)
print格式:print item1, item2, ...
省略item,至關於print $0
!~:是否不匹配事件
示例:
awk -F: '$0 ~ /root/{print $1}‘ /etc/passwd
awk '$0~「^root"' /etc/passwd
awk '$0 !~ /root/‘ /etc/passwd
awk -F: ‘$3==0’ /etc/passwd操做符
邏輯操做符:與&&(
與運算,注意不是
短路與),或||,非!
示例:
• awk -F: '$3>=0 && $3<=1000 {print $1}' /etc/passwd
• awk -F: '$3==0 || $3>=1000 {print $1}' /etc/passwd
• awk -F: ‘!($3==0) {print $1}' /etc/passwd
• awk -F: ‘!($3>=500) {print $3}’ /etc/passwd
條件表達式(三目表達式)
selector?if-true-expression:if-false-expression
• 示例:
awk -F: '{$3>=1000?usertype="Common User":usertype="
SysUser";printf "%15s:%-s\n",$1,usertype}' /etc/passwdawk PATTERN
PATTERN:根據pattern條件,過濾匹配的行,再作處理
(1)若是未指定:空模式,匹配每一行
(2) /regular expression/:僅處理可以模式匹配到的行,須要用/ /括起來
awk '/^UUID/{print $1}' /etc/fstab
awk '!/^UUID/{print $1}' /etc/fstab
![](http://static.javashuo.com/static/loading.gif)
/須要轉義字符串
兩種寫法it
![](http://static.javashuo.com/static/loading.gif)
過濾掉以空白符以及#開頭的行
(3) relational expression: 關係表達式,結果爲「真」纔會被處理
真:結果爲非0值,非空字符串
假:結果爲空字符串或0值
非0 或空字符取反爲1爲假
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
取反
變量
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
awk PATTERN
4) line ranges:行範圍
startline,endline:/pat1/,/pat2/ 不支持直接給出數字格式
\>單詞右側錨定
awk -F: ‘/^root\>/,/^nobody\>/{print $1}' /etc/passwd
awk -F: ‘(NR>=10&&NR<=20){print NR,$1}' /etc/passwd 顯示行號
(5) BEGIN/END模式
BEGIN{}:僅在開始處理文件中的文本以前執行一次
END{}:僅在文本處理完成以後執行一次
i變量未賦值爲空
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
awk控制語句if-else
語法:if(condition){statement;…}[else statement]
if(condition1){statement1}else if(condition2){statement2}else{statement3}
使用場景:對awk取得的整行或某個字段作條件判斷
![](http://static.javashuo.com/static/loading.gif)
打印奇數行
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
awk控制語句
while循環
語法:while(condition){statement;…}
條件「真」,進入循環;條件「假」,退出循環
使用場景:
行自動循環 針對於列
對一行內的多個字段逐一相似處理時使用
對數組中的各元素逐一處理時使用
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
time 統計時間
awk 運算效率最高
![](http://static.javashuo.com/static/loading.gif)
break和continue
awk ‘BEGIN{sum=0;for(i=1;i<=100;i++)
{if(i%2==0)continue;sum+=i}print sum}'
awk ‘BEGIN{sum=0;for(i=1;i<=100;i++)
{if(i==66)break;sum+=i}print sum}'
break 循環中止
next:
提早結束對本行處理而直接進入下一行處理(awk自身循環)
awk -F: '{if($3%2!=0) next; print $1,$3}' /etc/passwd
awk數組
關聯數組:array[index-expression]
不需聲明 直接賦值
index-expression:
• (1)
可以使用任意字符串;字符串要使用雙引號括起來
• (2) 若是某數組元素事先不存在,在引用時,awk會自動建立此元素,並將其值 初始化爲「空串」
• (3) 若要判斷數組中是否存在某元素,要使用「index in array」格式進行遍歷
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)