awk的使用方法

來源:互聯網
上載者:User

標籤:awk的使用方法

                             AWK用法一、知識點一1、域分割 -F 確定分割符 如果不加-F 預設以空格分割符 cat /etc/passwd | awk -F: ‘{print $1,$2,$3}‘2、抽取域cat /etc/passwd | awk -F: ‘{print $1}‘3、$0代表全域域,也就是所有的記錄cat /etc/passwd | awk -F: ‘{print $0}‘4、單獨列印記錄cat /etc/passwd | awk -F: ‘{print $1,$4}‘5、列印報告頭# cat /etc/passwd | awk -F: ‘BEGIN{print "This is system user"}{print $1}‘ | more6、列印資訊尾# cat /etc/passwd | awk -F: ‘BEGIN{print "This is system user"}{print $1}END{print "==================="}‘二、知識點二awkRegex(1) ~匹配cat /etc/passwd | awk ‘{if($0~/root/) print $0}‘(2) 精確匹配cat /etc/passwd | awk -F: ‘{if($1~/root/) print $1}‘(3) 不匹配!cat /etc/passwd | awk ‘$0 !~/root/‘cat /etc/passwd | awk -F: ‘{if($1~/root/)print $1}‘(4) 小於cat /etc/passwd |awk -F: ‘{if($3<200)print $3}‘(5) 小於或者等於cat /etc/passwd | awk -F: ‘{if($3<=200)print $3}‘(6) 大於或者等於cat /etc/passwd | awk -F: ‘{if($3>=200)print $3}‘(7) 匹配多個關鍵字 cat /etc/passwd | awk -F: ‘$1~/(root|user)/‘(8) 匹配行首cat /etc/passwd | awk -F: ‘$1~/^root/‘(9) awk 使用&& || 條件1為真&&則條件二執行條件1為真|| 則條件二不執行cat /etc/passwd | awk -F: ‘{if($1=="root" && $5=="root")print $0}‘cat /etc/passwd | awk -F: ‘{if($1=="root" || $1=="sdfsfsdsdsd") print $0}‘awk NF 和NRNF 瀏覽記錄域的個數NR 已讀的記錄數 cat /etc/passwd | awk -F: ‘{print NF}‘cat /etc/passwd | awk -F: ‘{print $1,NR}‘cat /etc/passwd | awk -F: ‘{if(NR<10 && $1~/root/)print $1}‘awk 替換gsub(r,s)cat /etc/passwd | awk -F: ‘gsub(/root/,"alvinzeng"){print $0}‘本章練習題:1 awk 預設以什麼為分割符?空格 2 抽取/etc/passwd 第一個域和第5個域並且統計他們的行號cat passwd |awk -F: ‘{print $1,$5,NR}‘ 3 抽取第一個域,並且進行root匹配,然後列印出報告頭內容為"This is user"cat passwd |awk -F: ‘BEGIN{print "this is user"}{if($1~/root/)print $1}‘4 抽取最後一個域,尾部資訊用“########”分割cat passwd|awk -F: ‘{print $NF}END{print "========="}‘5 請抽取第一個域,請且匹配以user開頭的關鍵字cat passwd |awk -F: ‘{if($1~/^user/)print $1}‘6 匹配全域域帶有[0-9]的關鍵字的域資訊cat passwd |awk -F: ‘$0~/[0-9]/‘7 請列印出來行後大於30 並且匹配第一個域為mysql的資訊條目cat passwd |awk -F: ‘{if(NR>30 && $1=="mysql")print $0}‘8 請列印出/etc/passwd 裡面最後一個域,並且統計bash有多少個?nologin的有多少個?cat paasswd |awk -F: ‘{if($NF~/bash$/)print $NF}‘|wc -l9 請列印出UID小於500 但是大於200的使用者資訊cat passwd |awk -F: ‘{if($3>200 && $3<500)print $0}‘10 請列印出帶DHCP關鍵子的使用者資訊,並且告訴使用者在那一行?cat passwd|awk -F: ‘{if($0~/DHCP/)print $0,NR}‘11 請列印出wu開頭的使用者有幾個?cat passwd |awk -F: ‘{if($0~/^wu/)print $0}‘12 抽取全域匹配home關鍵字的有多少個使用者?他們是否具bash登入許可權cat passwd |awk -F: ‘{if($0~/home/ && $NF~/bash$/)print $0,$NF}‘13 匹配行號大於30 並且球UID大於50或者小於500的使用者資訊cat passwd |awk -F: ‘{if($3>50 && $3<500 && NR>30)print $0,NR}‘14 匹配系統使用者有多少個? 以及程式使用者有多少個?系統使用者和程式使用者的區別就是nologin和bash的區別15 匹配行號小於30的,並且匹配他們的bash的使用者有多少個,在輸出他們的行號?


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.