首页 > 代码库 > AWK Demo
AWK Demo
AWK is so poweful.
#!/bin/bash#File: script.sh#Author: lxw#Time: 2014-08-08
#Usage: ‘awk‘ demo.#$0: The whole line. $1: The first column. $2: The second column. ...echo -e "\nDEMO1:-----------------------------------------------------"head -n 5 ./demo.txt|awk ‘BEGIN{FS="|"}{print $0}‘echo -e "\nDEMO2: PRINTF FORMAT---------------------------------------"head -n 20 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"}{printf "%-8s%-5s%-10s\n", $1, $2, $4}‘echo -e "\nDEMO3: FILTER-----------------------------------------------"head -n 30 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $2=="CN" && $3=="ipv4" {printf "%-8s%-5s%-10s\n", $1, $2, $4}‘echo -e "\nDEMO4: FS-NR-NF-FILENAME------------------------------------"head -n 40 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $2=="CN" || NR==1 {printf "%-10s%-03d%-3d%-8s%-5s%-10s\n", FILENAME, NR, NF, $1, $2, $4}‘echo -e "\nDEMO5: OFS--------------------------------------------------"head -n 50 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $2=="CN" || NR==1 {print FILENAME, NR, NF, $1, $2, $4}‘ OFS="\t"#String match#@1:‘~‘ means to match. @2:The string between // is the RE.echo -e "\nDEMO6: String Match------------------------------------------"head -n 50 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $3 ~ /ip.4/ || NR==1 {print NR, $1, $2, $3, $4}‘ OFS="\t"echo -e "\nDEMO7: The line which contains the specific string.----------"head -n 60 ./demo.txt|tail -n 10|awk ‘/.*12.*|.*14.*/‘echo -e "\nDEMO8: The field which DOES NOT contain the specific string.--"head -n 60 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $6 !~ /.*0412.*|.*0414.*/‘#Split the file.echo -e "\nDEMO9: Split the file.----------------------------------------"head -n 70 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $2 != "" {print > $2}‘head -n 70 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} $2 != "" {print NR, $4 > $2"-1"}‘head -n 70 ./demo.txt|tail -n 10|awk ‘BEGIN{FS="|"} {if($2 ~ /CN/) print > "cn.txt"; else if($2 ~ /TH/) print > "th.txt"; else print > "others.txt";}‘#Count.echo -e "\nDEMO10: Count.------------------------------------------------"cat ./demo.txt|awk ‘BEGIN{FS="|"} $2 != "" {arr[$2]++}; END{for(i in arr) print i, ", ", arr[i];}‘#Reference:#http://coolshell.cn/?s=awk#http://xu020408.blog.163.com/blog/static/26548920132704942773/
The demo.txt is like this:
apnic|CN|ipv4|223.223.176.0|4096|20100813|allocated|A9294C05apnic|CN|ipv4|223.223.192.0|4096|20100806|allocated|A9237638apnic|JP|ipv4|223.223.208.0|2048|20100803|allocated|A92B1B09apnic|HK|ipv4|223.223.216.0|1024|20100730|allocated|A91AEBC2apnic|SG|ipv4|223.223.220.0|1024|20110411|allocated|A9198FADapnic|JP|ipv4|223.223.224.0|8192|20100730|allocated|A924F5F3apnic|IN|ipv4|223.224.0.0|1048576|20100914|allocated|A9199197apnic|CN|ipv4|223.240.0.0|524288|20100803|allocated|A9248097apnic|CN|ipv4|223.248.0.0|262144|20100713|allocated|A92869B9apnic|AU|ipv4|223.252.0.0|32768|20100727|allocated|A91776C8apnic|CN|ipv4|223.252.128.0|32768|20110131|allocated|A92E14F7apnic|KR|ipv4|223.253.0.0|65536|20100728|allocated|A925D762apnic|CN|ipv4|223.254.0.0|65536|20100723|allocated|A92FD265apnic|CN|ipv4|223.255.0.0|32768|20100810|allocated|A92FEB6Fapnic|HK|ipv4|223.255.128.0|16384|20100810|allocated|A9132B1Eapnic|KR|ipv4|223.255.192.0|8192|20100802|allocated|A9249F52apnic|ID|ipv4|223.255.224.0|2048|20100809|allocated|A92ED542apnic|AU|ipv4|223.255.232.0|1024|20100812|allocated|A9190386apnic|CN|ipv4|223.255.236.0|1024|20110311|allocated|A929E12Capnic|HK|ipv4|223.255.240.0|1024|20100803|allocated|A91846F6apnic|IN|ipv4|223.255.244.0|1024|20100804|allocated|A92523BFapnic||ipv4|223.255.248.0|1024||reserved|apnic|CN|ipv4|223.255.252.0|512|20110414|allocated|A92E1062apnic|SG|ipv4|223.255.254.0|256|20110408|assigned|A91B8C6Capnic|AU|ipv4|223.255.255.0|256|20110811|assigned|A9173591apnic|JP|ipv6|2001:200::|35|19990813|allocated|A916B6AAapnic|JP|ipv6|2001:200:2000::|35|20030423|allocated|A916B6AAapnic|JP|ipv6|2001:200:4000::|34|20030423|allocated|A916B6AAapnic|JP|ipv6|2001:200:8000::|33|20030423|allocated|A916B6AAapnic||ipv6|2001:201::|32||available|apnic||ipv6|2001:202::|31||available|apnic||ipv6|2001:204::|30||available|apnic|SG|ipv6|2001:208::|35|19990827|allocated|A919DB08apnic|SG|ipv6|2001:208:2000::|35|20030306|allocated|A919DB08apnic|SG|ipv6|2001:208:4000::|34|20030306|allocated|A919DB08apnic|SG|ipv6|2001:208:8000::|33|20030306|allocated|A919DB08apnic||ipv6|2001:209::|32||available|apnic||ipv6|2001:20a::|31||available|apnic||ipv6|2001:20c::|30||available|apnic||ipv6|2001:210::|35||reserved|apnic||ipv6|2001:210:2000::|35||available|apnic||ipv6|2001:210:4000::|34||available|apnic||ipv6|2001:210:8000::|33||available|apnic||ipv6|2001:211::|32||available|apnic||ipv6|2001:212::|31||available|apnic||ipv6|2001:214::|30||available|apnic|JP|ipv6|2001:218::|35|19990922|allocated|A91D8D8Aapnic|JP|ipv6|2001:218:2000::|35|20020724|allocated|A91D8D8Aapnic|JP|ipv6|2001:218:4000::|34|20020724|allocated|A91D8D8Aapnic|JP|ipv6|2001:218:8000::|33|20020724|allocated|A91D8D8Aapnic||ipv6|2001:219::|32||available|apnic||ipv6|2001:21a::|31||available|apnic||ipv6|2001:21c::|30||available|apnic|KR|ipv6|2001:220::|35|19991006|allocated|A92B4E0Eapnic|KR|ipv6|2001:220:2000::|35|20031126|allocated|A92B4E0Eapnic|KR|ipv6|2001:220:4000::|34|20031126|allocated|A92B4E0Eapnic|KR|ipv6|2001:220:8000::|33|20031126|allocated|A92B4E0Eapnic||ipv6|2001:221::|32||available|apnic||ipv6|2001:222::|31||available|apnic||ipv6|2001:224::|30||available|apnic||ipv6|2001:228::|35||reserved|apnic||ipv6|2001:228:2000::|35||available|apnic||ipv6|2001:228:4000::|34||available|apnic||ipv6|2001:228:8000::|33||available|apnic||ipv6|2001:229::|32||available|apnic||ipv6|2001:22a::|31||available|apnic||ipv6|2001:22c::|30||available|apnic|KR|ipv6|2001:230::|35|19991124|allocated|A9267E82apnic|KR|ipv6|2001:230:2000::|35|20020802|allocated|A9267E82apnic|KR|ipv6|2001:230:4000::|34|20020802|allocated|A9267E82apnic|KR|ipv6|2001:230:8000::|33|20020802|allocated|A9267E82apnic||ipv6|2001:231::|32||available|apnic||ipv6|2001:232::|31||available|apnic||ipv6|2001:234::|30||available|apnic|TW|ipv6|2001:238::|35|20000208|allocated|A912C007apnic|TW|ipv6|2001:238:2000::|35|20020711|allocated|A912C007apnic|TW|ipv6|2001:238:4000::|34|20020711|allocated|A912C007apnic|TW|ipv6|2001:238:8000::|33|20020711|allocated|A912C007apnic||ipv6|2001:239::|32||available|apnic||ipv6|2001:23a::|31||available|apnic||ipv6|2001:23c::|30||available|
Reference:
AWK简明教程: http://coolshell.cn/articles/9070.html
AWK简明教程: http://xu020408.blog.163.com/blog/static/26548920132704942773/
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。