AWK命令的使用汇总
来源:互联网 发布:知乎网址 编辑:程序博客网 时间:2024/06/02 19:41
Section 1: 常用运维awk命令统计tomcat每秒的带宽(字节),最大的排在最后面#cat localhost_access_log.txt | awk '{ bytes[$5] += $NF; }; END{for(time in bytes) print bytes[time] " " time}' | sort -n统计某一秒的带宽#grep "18:07:34" localhost_access_log.txt |awk '{ bytes += $NF; } END{ print bytes }'统计指定ip.txt中ip在local_access.txt中出现的次数#cat ip.txt //内容如下 12.3.4.512.3.4.612.3.4.712.3.4.8#cat local_access.txt19:23:35 /a.html 12.3.4.519:23:35 /b.html 12.3.4.519:23:35 /c.html 12.3.4.619:23:35 /d.html 12.3.4.719:23:35 /a.html 12.3.4.919:23:35 /b.html 12.3.4.919:23:35 /c.html 12.3.4.9#awk -F " " '{if (NR==FNR) {arr1[$1]=1} else{arr2[$3]++;}} END{for(ip in arr1){print ip,arr2[ip]}}' ip.txt local_access.txt12.3.4.5 212.3.4.6 112.3.4.7 112.3.4.8Section 2: $0,$1,$2,$NF,$(NF-1)的使用$0整个当前行, $1当前行的第一个域 $NF为最后一个域 $(NF-1)为倒数第二个#echo "a b c d e" |awk '{print $1; print $2; print $(NF-1);print $NF;print $0}'a //对应第1个域b //对应第2个域d //对应$(NF-1),对应倒数第二个域e //对应$NF,最后一个域a b c d e //对应$0Section 3: print, printf用法#awk 'BEGIN{a=1;b="213";print "output "a","b;}'output 1,213#awk 'BEGIN{a=1;b="213";print "output",a,","b;}'output 1 ,213printf的使用#awk 'BEGIN{a=1;b="213";printf("output %d,%s\n",a,b)}'output 1,213Section 4: 选择分隔符awk默认是按照空格来分割, NF表示域的个数#echo "a:b c,d" |awk '{print $1; print $2; print NF}'a:bc,d2根据":",空格,","来进行分割#echo "a:b c,d" |awk -F " |,|:" '{print $1; print $2; print NF}'ab4Section 5: BEGIN,END用法abc.txt内容如下:first ladysecond boythird child#cat abc.txt |awk 'BEGIN {print "begin process"} {print "process 1 "$1} {print "process 2 "$2} END { print " the end"}'换行后如下:#cat abc.txt |awk -F " " ' 'BEGIN {print "begin process"} //在开头的时候执行一次 {print "process 1 "$1} //每一行执行一次 {print "process 2 "$2} //每一行执行一次 END { print " the end"}' //最后的时候执行一次输出如下begin process process 1 first // {print "process 1 "$1} 执行了一次process 2 lady // {print "process 2 "$2} 执行了一次process 1 secondprocess 2 boyprocess 1 thirdprocess 2 child the end没有BEGIN,只有END的情况#cat abc.txt |awk '{print "begin process"} {print "process 1 "$1} {print "process 2 "$2} END { print " the end"}' 格式化语句如下#cat abc.txt |awk -F ":" '{print "begin process"} //因为没有BEGIN 所以这个每一行都会执行 {print "process 1 "$1} //每一行都会执行 {print "process 2 "$2} //每一行都会执行 END { print " the end"}' //最后执行一次输出如下: begin processprocess 1 firstprocess 2 ladybegin processprocess 1 secondprocess 2 boybegin processprocess 1 thirdprocess 2 child the endSection 6: 数组使用awk中数据结构使用, 数组也可以理解为map#awk 'BEGIN{array1["a"]=1;array1[2]="213";print array1["a"],array1[2]}'1 213year.txt中内容如下 2016:09 1 //表示2016年9月,有一个访问2016:06 12016:06 12016:01 12015:01 12014:01 12015:01 12016:02 1下面语句是把每个月的访问量相加,排序后输出#awk '{bytes[$1]+=$2} END { for(time in bytes) print bytes[time],time}' year.txt |sort -n展开如下#awk '{bytes[$1]+=$2} //bytes为数组,下标是时间,value是访问量 END { for(time in bytes) print bytes[time], time }' year.txt |sort -n输出的内容如下; bytes是一个数组,下标是字符串""上面用数组,下标可以是数字,也可以是字符串1 2014:011 2016:011 2016:021 2016:092 2015:012 2016:06#awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!="1"){print "no found";};for(k in tB){print k,tB[k];}}' 展开是如下#awk 'BEGIN{ tB["a"]="a1"; tB["b"]="b1"; if(tB["c"]!="1"){ //这个地方会判断在里面,但是会往tB占用一个值 print "no found"; }; for(k in tB){ print k,tB[k]; } }' 输出如下:no founda a1b b1c 很奇怪, “c”没有赋值,循环的时候,就发现在里面了,这个里面有副作用要修改这个点,需要使用如下#awk 'BEGIN { tB["a"]="a1"; tB["b"]="b1"; if ("c" in tB) { //用这个来进行判断,就没有负作用 print "c is in tB"; } for(k in tB){ print k,tB[k]; } }'awk的多维数组#awk 'BEGIN{ for(i=1;i<=3;i++) {for(j=1;j<=3;j++) {tarr[i,j]=i*j;print i,"*",j,"=",tarr[i,j]}}}'展开后如下:#awk 'BEGIN{ for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { tarr[i,j]=i*j; print i,"*",j,"=",tarr[i,j] } } }'输出如下:1 * 1 = 11 * 2 = 21 * 3 = 32 * 1 = 22 * 2 = 42 * 3 = 63 * 1 = 33 * 2 = 63 * 3 = 9awk多维数组的in判断awk 'BEGIN{ tarr[1,3]=5;if ((1,3) in tarr) print "1,3 in"; if ((4,4) in tarr) print "4,4 in"}'#awk 'BEGIN{ tarr[1,3]=5; if ((1,3) in tarr) //直接使用(1,3)来判断in语句 print "1,3 in"; if ((4,4) in tarr) print "4,4 in"}'Section 7: for语句使用#awk 'BEGIN{array1["a"]=1;array1["c"]=3;array1["b"]=2;for(index1 in array1) print index1,array1[index1]}'展开如下:#awk 'BEGIN{ array1["a"]=1; array1["c"]=3; array1["b"]=2; for(index1 in array1) print index1,array1[index1] }'输出如下:a 1b 2c 3for也可以使用k=1;k<=3;k++的形式#awk 'BEGIN{array1[1]="a";array1[3]="c";array1[2]="b";len=length(array1);for(k=1;k<=len;k++) print k,array1[k]}'展开如下:#awk 'BEGIN{ array1[1]="a"; array1[3]="c"; array1[2]="b"; len=length(array1); //得到数组的长度 for(k=1;k<=len;k++) print k,array1[k] }'输出如下1 a2 b3 cSection 8: 内置函数使用int函数,把字符串转为整数#awk 'BEGIN {print int("12.9")}' 返回一个整数12 index函数 #awk 'BEGIN {print index("12.9343",".")}' //index方法返回"."在"12.9343的位置,没有找到则返回03length函数 得到数组的长度,字符串长度#awk 'BEGIN{array1["a"]=1;array1["b"]=2;print length(array1)}'输出如下:2#awk 'BEGIN{a="123";print length(a)}' 得到字符串长度3match函数, 检测info中是否含有"te" 如果有,则返回"te"第一次出现的位置,如果没有则返回0#awk 'BEGIN {info="is is test"; print match(info,"te");}'rand函数 生成随机数 但是事实上是不随机的#awk 'BEGIN {print rand " " rand}' rand会生成一个0-1的数字0.840188 0.394383 //每次运行第一个,第二个都是这个数字split函数 按照某个分隔符,对字符串进行分割split按照" "对"it is a test"进行切割,切割后的内容放在thearray中 返回的是split后,thearray的元素个数,#awk 'BEGIN {print split("it is a test",thearray," "); print thearray[1]}'4 //split后返回数组的长度it //打印第一个元素sub函数 替换#awk 'BEGIN {info="this a test"; sub("a","b",info); print info }' 把info中"a"用"b"替代this b testsubstr函数 得到子字符串substr(s, m, n) s是要截取的字符串,m是开始点,从1开始, n是要截取的长度#awk 'BEGIN {print substr("12.9343",2,4)}' //substr2.93toupper函数 字符串转为大写#awk 'BEGIN {info="this a test"; print toupper(info);}'THIS A TESTtolower函数 字符串转为消协#awk 'BEGIN {info="thIS A TEST"; print tolower(info);}'this a test关于
阅读全文
0 0
- AWK命令的使用汇总
- awk 命令的使用
- awk命令的使用
- awk 命令的使用
- awk命令的的使用
- awk命令的使用(一):简介
- awk命令的使用(四)数组
- awk中使用shell命令的方法
- awk中使用的shell命令
- 使用awk批量杀进程的命令
- sed、cut和awk命令的使用
- awk中使用的shell命令-浅记
- linux AWK命令的一点使用见解
- 使用awk批量杀进程的命令
- 使用awk批量杀进程的命令
- 使用awk批量杀进程的命令
- linux 系统下awk命令的使用
- awk使用命令范例
- <天气>날씨
- 单调队列 JC loves Mkk
- Log4j学习记录
- Algorithms 练习1.4.6
- iOS AFNetworking 上传多张图片
- AWK命令的使用汇总
- 百度地图的定位
- LTE web
- sql server递归分页查询
- IDEA常用快捷键总结
- FFMPEG视频转码
- 挑战程序竞赛系列(70):4.7后缀数组(2)
- Angular最新教程-第五节编写第一个组件
- 学习笔记:ES6之Set-map