码迷,mamicode.com
首页 > 其他好文 > 详细

AWK Demo

时间:2014-08-09 13:11:27      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   strong   for   2014   art   

  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|A9294C05
apnic|CN|ipv4|223.223.192.0|4096|20100806|allocated|A9237638
apnic|JP|ipv4|223.223.208.0|2048|20100803|allocated|A92B1B09
apnic|HK|ipv4|223.223.216.0|1024|20100730|allocated|A91AEBC2
apnic|SG|ipv4|223.223.220.0|1024|20110411|allocated|A9198FAD
apnic|JP|ipv4|223.223.224.0|8192|20100730|allocated|A924F5F3
apnic|IN|ipv4|223.224.0.0|1048576|20100914|allocated|A9199197
apnic|CN|ipv4|223.240.0.0|524288|20100803|allocated|A9248097
apnic|CN|ipv4|223.248.0.0|262144|20100713|allocated|A92869B9
apnic|AU|ipv4|223.252.0.0|32768|20100727|allocated|A91776C8
apnic|CN|ipv4|223.252.128.0|32768|20110131|allocated|A92E14F7
apnic|KR|ipv4|223.253.0.0|65536|20100728|allocated|A925D762
apnic|CN|ipv4|223.254.0.0|65536|20100723|allocated|A92FD265
apnic|CN|ipv4|223.255.0.0|32768|20100810|allocated|A92FEB6F
apnic|HK|ipv4|223.255.128.0|16384|20100810|allocated|A9132B1E
apnic|KR|ipv4|223.255.192.0|8192|20100802|allocated|A9249F52
apnic|ID|ipv4|223.255.224.0|2048|20100809|allocated|A92ED542
apnic|AU|ipv4|223.255.232.0|1024|20100812|allocated|A9190386
apnic|CN|ipv4|223.255.236.0|1024|20110311|allocated|A929E12C
apnic|HK|ipv4|223.255.240.0|1024|20100803|allocated|A91846F6
apnic|IN|ipv4|223.255.244.0|1024|20100804|allocated|A92523BF
apnic||ipv4|223.255.248.0|1024||reserved|
apnic|CN|ipv4|223.255.252.0|512|20110414|allocated|A92E1062
apnic|SG|ipv4|223.255.254.0|256|20110408|assigned|A91B8C6C
apnic|AU|ipv4|223.255.255.0|256|20110811|assigned|A9173591
apnic|JP|ipv6|2001:200::|35|19990813|allocated|A916B6AA
apnic|JP|ipv6|2001:200:2000::|35|20030423|allocated|A916B6AA
apnic|JP|ipv6|2001:200:4000::|34|20030423|allocated|A916B6AA
apnic|JP|ipv6|2001:200:8000::|33|20030423|allocated|A916B6AA
apnic||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|A919DB08
apnic|SG|ipv6|2001:208:2000::|35|20030306|allocated|A919DB08
apnic|SG|ipv6|2001:208:4000::|34|20030306|allocated|A919DB08
apnic|SG|ipv6|2001:208:8000::|33|20030306|allocated|A919DB08
apnic||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|A91D8D8A
apnic|JP|ipv6|2001:218:2000::|35|20020724|allocated|A91D8D8A
apnic|JP|ipv6|2001:218:4000::|34|20020724|allocated|A91D8D8A
apnic|JP|ipv6|2001:218:8000::|33|20020724|allocated|A91D8D8A
apnic||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|A92B4E0E
apnic|KR|ipv6|2001:220:2000::|35|20031126|allocated|A92B4E0E
apnic|KR|ipv6|2001:220:4000::|34|20031126|allocated|A92B4E0E
apnic|KR|ipv6|2001:220:8000::|33|20031126|allocated|A92B4E0E
apnic||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|A9267E82
apnic|KR|ipv6|2001:230:2000::|35|20020802|allocated|A9267E82
apnic|KR|ipv6|2001:230:4000::|34|20020802|allocated|A9267E82
apnic|KR|ipv6|2001:230:8000::|33|20020802|allocated|A9267E82
apnic||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|A912C007
apnic|TW|ipv6|2001:238:2000::|35|20020711|allocated|A912C007
apnic|TW|ipv6|2001:238:4000::|34|20020711|allocated|A912C007
apnic|TW|ipv6|2001:238:8000::|33|20020711|allocated|A912C007
apnic||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/

AWK Demo,布布扣,bubuko.com

AWK Demo

标签:style   blog   http   color   strong   for   2014   art   

原文地址:http://www.cnblogs.com/lxw0109/p/AWK_DEMO.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!