鍥炲埌锛�
- Linux绯诲垪鏂囩珷
- Shell绯诲垪鏂囩珷
- Awk绯诲垪鏂囩珷
awk鍙橀噺
awk鐨勫彉閲忔槸鍔ㄦ�佸彉閲忥紝鍦ㄤ娇鐢ㄦ椂澹版槑銆�
鎵�浠wk鍙橀噺鏈�3绉嶇姸鎬侊細
- 鏈0鏄庣姸鎬侊細绉颁负untyped绫诲瀷
- 寮曠敤杩囦絾鏈祴鍊肩姸鎬侊細unassigned绫诲瀷
- 宸茶祴鍊肩姸鎬�
寮曠敤鏈祴鍊肩殑鍙橀噺锛屽叾榛樿鍒濆鍊间负绌哄瓧绗︿覆鎴栨暟鍊�0銆�
鍦╝wk涓湭澹版槑鐨勫彉閲忕О涓簎ntyped锛屽0鏄庝簡浣嗘湭璧嬪��(鍙寮曠敤浜嗗氨澹版槑浜�)鐨勫彉閲忓叾绫诲瀷涓簎nassigned銆�
gawk 4.2鐗堟彁渚涗簡typeof()
鍑芥暟锛屽彲浠ユ祴璇曞彉閲忕殑鏁版嵁绫诲瀷锛屽寘鎷祴璇曞彉閲忔槸鍚﹀0鏄庛��
awk 'BEGIN{
print(typeof(a)) # untyped
if(b==0){print(typeof(b))} # unassigned
}'
闄や簡typeof()锛岃繕鍙互浣跨敤涓嬮潰鐨勬妧宸ц繘琛屾娴嬶細
awk 'BEGIN{
if(a=="" && a==0){ # 鏈祴鍊兼椂锛屼袱涓兘true
print "untyped or unassigned"
} else {
print "assigned"
}
}'
鍙橀噺璧嬪��
awk涓殑鍙橀噺璧嬪�艰鍙ヤ篃鍙互鐪嬩綔鏄竴涓�鏈夎繑鍥炲��鐨勮〃杈惧紡銆�
渚嬪锛�a=3
璧嬪�煎畬鎴愬悗杩斿洖3锛屽悓鏃跺彉閲廰涔熻璁剧疆涓�3銆�
鍩轰簬杩欎釜鐗圭偣锛屾湁涓ょ偣鐢ㄦ硶锛�
- 鍙互
x=y=z=5
锛岀瓑浠蜂簬z=5 y=5 x=5
- 鍙互灏嗚祴鍊艰鍙ユ斁鍦ㄤ换鎰忓厑璁镐娇鐢ㄨ〃杈惧紡鐨勫湴鏂�
x != (y = 1)
awk 'BEGIN{print (a=4);print a}'
闂锛�a=1;arr[a+=2] = (a=a+6)
鏄�庝箞璧嬪�肩殑锛屽搴斿厓绱犵粨鏋滅瓑浜庯紵arr[3]=7
銆備絾涓嶈杩欎箞鍋氾紝鍥犱负涓嶅悓awk鐨勮祴鍊艰鍙ュ乏鍙充袱杈圭殑璇勪及椤哄簭鏈夊彲鑳戒笉鍚屻��
awk涓0鏄庡彉閲忕殑浣嶇疆
- 鍦˙EGIN鎴杕ain鎴朎ND浠g爜娈典腑鐩存帴寮曠敤鎴栬祴鍊�
- 浣跨敤
-v var=val
閫夐」锛屽彲瀹氫箟澶氫釜锛屽繀椤绘斁鍦╝wk浠g爜鐨勫墠闈�- 瀹冪殑鍙橀噺澹版槑鏃╀簬BEGIN鍧�
- 鏅�氬彉閲忥細
awk -v age=123 'BEGIN{print age}'
- 浣跨敤shell鍙橀噺璧嬪�硷細
awk -v age=$age 'BEGIN{print age}'
- 瀹冪殑鍙橀噺澹版槑鏃╀簬BEGIN鍧�
- 鍦╝wk浠g爜鍚庨潰浣跨敤
var=val
鍙傛暟- 瀹冪殑鍙橀噺澹版槑鍦˙EGIN涔嬪悗
awk '{print n}' n=3 a.txt n=4 b.txt
awk '{print $1}' FS=' ' a.txt FS=":" /etc/passwd
- 浣跨敤Shell鍙橀噺璧嬪�硷細
awk '{print age}' age=$age a.txt
- 瀹冪殑鍙橀噺澹版槑鍦˙EGIN涔嬪悗
awk涓娇鐢⊿hell鍙橀噺
瑕佸湪awk涓娇鐢⊿hell鍙橀噺锛屾湁涓夌鏂瑰紡锛�
1.鍦�-v閫夐」涓皢Shell鍙橀噺璧嬪�肩粰awk鍙橀噺
num=$(cat a.txt | wc -l)
awk -v n=$num 'BEGIN{print n}'
-v閫夐」鏄湪awk宸ヤ綔娴佺▼鐨勭涓�闃舵瑙f瀽鐨勶紝鎵�浠�-v閫夐」澹版槑鐨勫彉閲忓湪BEGIN{}銆丒ND{}鍜宮ain浠g爜娈典腑閮借兘鐩存帴浣跨敤銆�
3.鐩存帴鍦╝wk浠g爜閮ㄥ垎鏆撮湶Shell鍙橀噺锛屼氦缁橲hell瑙f瀽杩涜Shell鐨勫彉閲忔浛鎹�
num=$(cat a.txt | wc -l)
awk 'BEGIN{print '"$num"'}'
杩欑鏂瑰紡鏈�鐏垫椿锛屼絾鍙鎬ф渶宸紝鍙兘浼氬嚭鐜板ぇ閲忕殑寮曞彿銆�