每天一個小練習--awk基礎

awk模式匹配語法

/正則表達式/ {匹配後的操作}


#cat shell_recorder.awk

#! /bin/bash
# shell_recorder.awk
BEGIN {
  print "SHELL USAGE:"
}
 /bash/{++bash}
 /nologin/{++nologin}
END {
  print "We have "bash" bash users."
  print "We have "nologin" nologin users."
}

#awk -f shell_recorder.awk /etc/passwd

We have 4 bash users.

We have 29 nologin users.

--------------------------------------------------------------------------

也可以使用以下的方法得到同樣的結果:

#cat recorder.sh

#! /bin/bash
# recorder.sh
bash=`cat /etc/passwd | grep 'bash' | wc | awk '{print $1}'`
nologin=`cat /etc/passwd | grep 'nologin' | wc | awk '{print $1}'`
echo "We have $bash bash users."
echo "We have $nologin nologin users."

# sh recorder.sh

We have 4 bash users.

We have 29 nologin users.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章