首页 > 其他 > 详细

awk入门

时间:2017-01-21 18:24:34      阅读:273      评论:0      收藏:0      [点我收藏+]

ifconfig eth0 | grep ‘t a‘ | awk -F‘[: ]+‘ ‘{print $4}‘

‘[: ]+‘ 表示以单个或连续的冒号或空格或冒号空格组合作为分隔符

sed -ir ‘s/::*/ /g‘ file

cat file | awk ‘{print $1"+"$2}‘

cat file | awk ‘{print NF}‘   # number of field cat file | awk ‘{print $NF}‘

cat file | awk ‘{print $(NF-1)}‘  #取倒数第二列

awk ‘{print NR}‘ file file1 awk ‘{print FNR}‘ file file1   # 相对行数

awk  ‘NR==FNR{print $0}‘ file file1 awk  ‘NR!=FNR{print $0}‘ file file1 cat file | awk ‘NR>1&&NR<4{print $0}‘

cat file | awk ‘NR==3{print $0}‘  # 输出第三行 cat file | awk ‘NR==3{print $1}‘  # 输出第三行第一列

cat file|awk ‘{a=0}a{print $1}‘ cat file|awk ‘{a=1}a{print $1}‘

cat file|awk ‘NR==1{print $1}NR==2{print $2}‘ #输出第一行第一列和第二行第二列

cat file|awk ‘BEGIN{print "========Grade======"}{print $0}END{print "-----Tail-----"}‘

awk ‘pattern1{action1}pattern2{action2}...‘

cat file|awk ‘{print $0,$3+$4+$5,($3+$4+$5)/3}‘  #计算总和与平均数

cat file|awk ‘{a=$3+$4+$5;print $0,a,a/3}‘

awk计算:

cat 10.txt|awk ‘{a+=$1}END{print a}‘

ps aux|grep sbin|awk ‘{a+=$3}END{print a}‘  #将第三列的值累加到a上

cat file|awk ‘{if($3>=80){print $0}}‘

ll|awk ‘/^-/{if($5>24){print $0}}‘

cat file|awk ‘{if(NR==1){print $0}}‘ 等价于 cat file|awk ‘NR==1{print $0}‘

cat file|awk -va=$a ‘{print a}‘

#for循环 cat file|awk ‘{for(i=1;i<NF;i++){print $i}}‘

#读取奇数行 cat file|awk ‘{for(i=1;i<=NF;i+=2){printf $i" "}{print xxoo}}‘ 等价于 cat file|awk ‘{for(i=1;i<=NF;i+=2){printf $i" "}{printf "\n"}}‘

cat file|awk ‘{if($1=="ll"){print $0}else{$1="ll";print $0}}‘

awk ‘BEGIN{while("who"|getline)n++;print n}‘ 等价于 who|awk ‘{a++}END{print a}‘

who|awk ‘{a++}END{print a}‘ > xxx.txt

# sed取IP ifconfig eth0|grep ‘t a‘|sed -n ‘s/^.*r:\(.*\) Bc.*$/\1/p‘

cat file|awk ‘$3~/90/{print $0}‘

cat file|awk ‘/^ll/||$3~/60/{print $0}‘

awk入门

原文:http://www.cnblogs.com/datapool/p/6337445.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!