热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

awk使用实例详解

一、print相关实例[root@localhost~]#tail-4etcfstab|awk{print$2,$4}650)this.width650

一、print相关实例

    [root@localhost ~]#tail -4 /etc/fstab | awk  '{print $2,$4}'

wKioL1ctbTijVtyKAABNIUgmmCQ157.png

    [root@localhost ~]#tail -4 /etc/fstab | awk  '{print"hello:"$1}'

    [root@localhost ~]#tail -4 /etc/fstab | awk  '{print"hello:" $1}'

wKiom1ctbF_QTe2pAAEEqB_Lr1s761.png

    [root@localhost ~]#tail -4 /etc/fstab | awk  '{print"hello:$1"}'

wKioL1ctbTnA1lvoAAA81PGVf7E398.png

    [root@localhost ~]#tail -4 /etc/fstab | awk  '{print"hello:"$1 1234567}'

    [root@localhost ~]#tail -4 /etc/fstab | awk  '{print"hello:"$1,1234567}'

wKioL1ctbTnjqJ6dAAEmxsZgDIk157.png

 

 

 

二、变量使用实例

     -F:等于-v FS=':'

    [root@localhost ~]#tail -3 /etc/passwd | awk -F: '{print $1 }'

    [root@localhost ~]#tail -3 /etc/passwd | awk -v FS=':' '{print $1 }'

wKiom1ctbGCDLPLVAABcorisf64953.png

    [root@localhost ~]#tail -3 /etc/passwd | awk -v FS=':' '{print $1,$3,$7 }'

    [root@localhost ~]#tail -3 /etc/passwd | awk -v FS=':' -v OFS='@' '{print $1,$3,$7 }'

wKiom1ctbGCiElGEAACd1nDi1hw067.png

    [root@localhost ~]#head -5 /etc/fstab | awk '{print NF}'

    [root@localhost ~]#head -5 /etc/fstab | awk '{print $NF}'

wKioL1ctbTriqAfjAABicidZ7zw780.png

    [root@localhost ~]#tail -4 /etc/fstab | awk '{print NR}'

wKiom1ctbGHTPz26AAAsVdrez5A921.png

    [root@localhost ~]#awk '{print FILENAME}' /etc/fstab /etc/issue

wKioL1ctbTrT-HhjAABvJGRt1A4901.png

    [root@localhost ~]#awk 'BEGIN{print ARGC}' /etc/fstab /etc/issue

    [root@localhost ~]#awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/issue

    [root@localhost ~]#awk 'BEGIN{print ARGV[1]}' /etc/fstab /etc/issue

    [root@localhost ~]#awk 'BEGIN{print ARGV[2]}' /etc/fstab /etc/issue

wKioL1ctbTrQDPJLAACgRNj84Ck792.png

    [root@localhost ~]#awk -v test='hello world' 'BEGIN{print test}'

    [root@localhost ~]#awk 'BEGIN{test="hello world";print test}'

wKiom1ctbGGTXDarAABPNUmepq4127.png

 

 

 

三、printf实例

    [root@localhost ~]# tail -3 /etc/passwd| awk -F: '{printf "%s",$1}'    # 默认在一行显示

    [root@localhost ~]#tail -3 /etc/passwd | awk -F: '{printf "%s\n",$1}'

    [root@localhost ~]#tail -3 /etc/passwd | awk -F: '{printf "Username:%s\n",$1}'

wKioL1ctbTvxfuTYAACTBPCtUKA655.png

    [root@localhost ~]#tail -3 /etc/passwd | awk -F: '{printf "Username:%s,UID:%d\n",$1,$3}'

wKiom1ctbGLww9yeAABUvOf1Yxw187.png

    [root@localhost~]#  head -3 /etc/passwd | awk -F:'{printf "Username: %10s, UID:%d\n",$1,$3}'

    [root@localhost~]#  head -3 /etc/passwd | awk -F:'{printf "Username: %-10s, UID:%d\n",$1,$3}'

wKiom1ctbGLi_2iJAACfOXRhjsY955.png

 

 

 

四、条件判断操作符实例

    [root@localhost ~]#tail -5 /etc/passwd | awk -F: '{$3>=1000?usertype="CommonUser":usertype="Sysadmin or SysUser";printf"%-15s:%-s\n",$1,usertype}'

wKioL1ctbTzh8AZzAACYG9HDfg8084.png

 

 

五、PATTERN使用实例

    [root@localhost ~]#awk '/^UUID/ {print $1}' /etc/fstab

wKioL1ctbTyRI3ZsAAB6JHsZyps546.png

    [root@localhost ~]#awk '!/^UUID/ {print $1,$2}' /etc/fstab

wKiom1ctbGOQJ7UeAAA9ljX4WWI325.png

    [root@localhost ~]#awk -F: '$3>=900{printf "%-30s,%d\n",$1,$3}' /etc/passwd

wKiom1ctbGPA-nLkAACFUxqYnxk473.png

    [root@localhost ~]#awk -F: '$NF=="/bin/bash"{printf "%-20s%s\n",$1,$NF}'/etc/passwd

    [root@localhost ~]#awk -F: '$NF~/bash$/{printf"%-20s%s\n",$1,$NF}' /etc/passwd

wKioL1ctbT3yw2zJAABqETtp-8E332.png

    [root@localhost ~]#awk -F: '(NR>=10&&NR<=15){print $1}' /etc/passwd

wKiom1ctbGTxiYO9AABMAFF7X6U158.png

    [root@localhost ~]#tail -5 /etc/passwd | awk -F: 'BEGIN{print "username            uid      \n-----------------------------"}{printf"%-20s%d\n",$1,$3}'

wKioL1ctbT2SyoAhAABvjBt4tqc249.png

    [root@localhost ~]#tail -5 /etc/passwd | awk -F: '{print "username            uid      \n-----------------------------";printf"%-20s%d\n",$1,$3}'

wKiom1ctbGSha_plAACRoMyWQxU057.png

    [root@localhost~]#  tail -5 /etc/passwd | awk -F:'BEGIN{print "username           uid      \n-----------------------------"}{printf"%-20s%d\n",$1,$3}END{print"===================\n end"}'

wKioL1ctbT6jtlXDAAB7Z33lG-Q156.png

 

 

六、if-else语句

    [root@localhost ~]#awk -F: '{if($3>=900) print $1,$3}' /etc/passwd

wKioL1ctbT7i8d0-AAB753hf_sg600.png

    [root@localhost ~]#tail -5 /etc/passwd | awk -F: '{if($3>=1000) {printf "Commonuser:%-34sUid:%d\n",$1,$3} else {printf "Root orSysuser:%-30sUid:%d\n",$1,$3}}'

wKiom1ctbGXw7zZzAACj42w1lZc777.png

    [root@localhost ~]#awk -F: '{if($NF=="/bin/bash") print $1}' /etc/passwd

wKiom1ctbGXzq1T6AAAygfmoy0w131.png

    [root@localhost ~]#df -h

    [root@localhost~]#  df -h | awk -F[%] '/^\/dev/{print$1}' | awk '{if($NF>=20) print $1}'

wKioL1ctbT_Q4xyGAADmqkhxo8s942.png

 

 

七、while循环

    [root@localhost ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF) {print $i,length($i); i++}}'/etc/grub2.cfg

wKioL1ctbT_iezvoAADVwYrWr7Y730.png

    [root@localhost~]#  awk'/^[[:space:]]*linux16/{i=1;while(i<=NF) {if(length($i)>=7) {print$i,length($i)};

wKiom1ctbGaSHD7xAADIu0VQRO4897.png

 

 

八、for循环

    [root@localhost ~]#awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}'/etc/grub2.cfg

wKioL1ctbUCDP6K-AADgjdU56g4912.png

 

 

九、next   

    [root@localhost ~]#tail -10 /etc/passwd | awk -F: '{if($3%2!=0) next; print $1,$3}'

wKiom1ctbGeQEgQ_AABW2bjuAv8768.png

 

 

十、数组

    [root@localhost ~]#netstat -tan | awk '/^tcp\>/{state[$NF]++}END{for(i in state) { printi,state[i]}}'

wKiom1ctbGfDbZO5AAATk3TzFDo457.png

    [root@localhost ~]#awk '/^UUID/{fs[$3]++}END{for(i in fs) {print i,fs[i]}}' /etc/fstab

wKiom1ctbjWi3LurAAAPVUmrcKg413.png

    [root@localhost ~]# awk'{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}'/etc/fstab    # 遍历文件统计单词出现次数

wKioL1ctbw6C5Bg0AABGh_d6rfQ800.png

 

 

十一、函数

    [root@localhost ~]#netstat -tan | awk'/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for (i in count){print i,count[i]}}'

wKiom1ctbjWRDLu7AAAT4-KRLqA167.png

本文出自 “许鼎的博客” 博客,请务必保留此出处http://xuding.blog.51cto.com/4890434/1770983


推荐阅读
author-avatar
qqqq
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有