池州网站优化,wordpress amp改成mip,17网一起做网店下载,自建网站卖东西目录
awk高级用法
awk控制语句—if-else判断
awk控制语句—while循环
awk控制语句—do-while循环
awk控制语句—for循环
shell脚本中较相似的控制语句
break和continue
next
awk数组
awk自定义函数
awk中调用shell 命令 这一篇主要介绍awk的高级用法#xff0c;因为…目录
awk高级用法
awk控制语句—if-else判断
awk控制语句—while循环
awk控制语句—do-while循环
awk控制语句—for循环
shell脚本中较相似的控制语句
break和continue
next
awk数组
awk自定义函数
awk中调用shell 命令 这一篇主要介绍awk的高级用法因为awk可以单独作为一门语言来使用所以它有很多高级用法
awk高级用法
awk控制语句—if-else判断
1语法
if(condition){statement;…}[else statement] 双分支
if(condition1){statement1}else if(condition2){statement2}else{statement3} 多分支
2使用场景对awk 取得的整行或某个字段做条件判断
3演示
演示文本awkdemo内容
hello:world
linux:redhat:lalala:hahaha
along:love:youou---打印出了如果/etc/passwd下的第三个字段即uid大于3小于1000的第1列和第3列
[rootcentos111 test]# awk -F: {if($310 $31000)print $1,$3} /etc/passwd
operator 11
games 12
...---打印出如果最后一列是bin/bash的则打印出第一列和最后一列
[rootcentos111 test]# awk -F: {if($NF/bin/bash)print $1,$NF} /etc/passwd
root /bin/bash
amandabackup /bin/bash
user /bin/bash
user1 /bin/bash
user2 /bin/bash---输出总列数大于3的行
[rootcentos111 test]# awk -F: {if(NF3) print $0} awkdemo
linux:redhat:lalala:hahaha---第3列1000为Common user反之是root or Sysuser
[rootcentos111 test]# awk -F: {if($31000){printf Conmmon user:%s\n,$1} else{printf root or Sysuser:%s\n,$1}} /etc/passwd
root or Sysuser:root
...
Conmmon user:nfsnobody
... ---磁盘利用率超过40的设备名和利用率
[rootcentos111 test]# df -h|awk -F% /^\/dev/{print $1}|awk $NF 40{print $1,$NF}
/dev/mapper/centos-root 56---test100和90为very good; 90test60为good; test60为no pass
[rootcentos111 test]# awk BEGIN{ test100;if(test90){print very good}else if(test60){ print good}else{print no pass}}
very good[rootcentos111 test]# awk BEGIN{ test80;if(test90){print very good}else if(test60){ print good}else{print no pass}}
good[rootcentos111 test]# awk BEGIN{ test50;if(test90){print very good}else if(test60){ print good}else{print no pass}}
no passawk控制语句—while循环
1语法
while(condition){statement;…}
注条件“真”进入循环条件“假” 退出循环
2使用场景 对一行内的多个字段逐一类似处理时使用 对数组中的各元素逐一处理时使用
3演示
---以along开头的行以为分隔显示每一行的每个单词和其长度
[rootcentos111 test]# awk -F: /^along/{i1;while(iNF){print $i,length($i); i}} awkdemo
along 5
love 4
youou 5
---以为分隔显示每一行的长度大于6的单词和其长度
[rootcentos111 test]# awk -F: {i1;while(iNF) {if(length($i)6){print $i,length($i)}; i}} awkdemo
redhat 6
lalala 6
hahaha 6---计算123...1005050
[rootcentos111 test]# awk BEGIN{i1;sum0;while(i100){sumi;i};print sum} 5050
5050awk控制语句—do-while循环
1语法
do {statement;…}while(condition)
意义无论真假至少执行一次循环体
2计算123...1005050
[rootcentos111 test]# awk BEGIN{sum0;i1;do{sumi;i}while(i100);print sum}
5050awk控制语句—for循环
1语法
for(expr1;expr2;expr3) {statement;…}
2特殊用法遍历数组中的元素
for(var in array) {for-body}
3演示
---显示每一行的每个单词和其长度
[rootcentos111 test]# awk -F: {for(i1;iNF;i){print$i,length($i)}} awkdemo
hello 5
linux 5
redhat 6
lalala 6
along 5
love 4
//NF表示字段数量
---求男m、女各自的平均
[rootalong ~]# cat sort.txt
xiaoming m 90
xiaohong f 93
xiaohei m 80
xiaofang f 99
[rootcentos111 test]# awk {m[$2];score[$2]$3}END{for(i in m) {printf %s:%6.2f\n,i,score[i]/m[i]}} sort.txt
m: 85.00
f: 96.00
shell脚本中较相似的控制语句
break和continue
---奇数相加
[rootcentos111 test]# awk BEGIN{sum0;for(i1;i100;i){if(i%20)continue;sumi}print sum}
2500---12...66
[rootcentos111 test]# awk BEGIN{sum0;for(i1;i100;i){if(i66)break;sumi}print sum}
2145---奇数相加
[rootcentos111 test]# awk BEGIN{sum0;for(i1;i100;i){if(i%20)continue;sumi}print sum}
2500---12...66
[rootcentos111 test]# awk BEGIN{sum0;for(i1;i100;i){if(i66)break;sumi}print sum}
2145next
next提前结束对本行处理而直接进入下一行处理awk 自身循环
---只打印偶数行
[rootcentos111 test]# awk -F: {if(NR%2!0) next; print $1,$3} /etc/passwd
bin 1
adm 3
...
awk数组
关联数组array[index-expression]
1可使用任意字符串字符串要使用双引号括起来
2如果某数组元素事先不存在在引用时awk 会自动创建此元素并将其值初始化为“空串”
3若要判断数组中是否存在某元素要使用“index in array”格式进行遍历
4若要遍历数组中的每个元素要使用for 循环for(var in array) {for-body}
演示
[rootcentos111 test]# cat awkdemo2
aaa
bbbb
aaa
123
123
123
---去除重复的行
[rootcentos111 test]# awk !arr[$0] awkdemo2
aaa
bbbb
123
---打印文件内容和该行重复第几次出现
[rootcentos111 test]# awk {!arr[$0];print $0,arr[$0]} awkdemo2
aaa 1
bbbb 1
aaa 2
123 1
123 2
123 3
分析把每行作为下标第一次进来相当于print ias...一样结果为空打印空!取反结果为1打印本行并且变为不空下次进来相同的行就是相同的下标本来上次的值取反为空不打印变为不空所以每次重复进来的行都不打印
2数组遍历
[rootcentos111 test]# awk BEGIN{abc[ceo]along;abc[coo]mayun;abc[cto]mahuateng;for(i in abc){print i,abc[i]}}coo mayun
ceo along
cto mahuateng
[rootcentos111 test]# awk {for(i1;iNF;i)abc[$i]}END{for(j in abc)print j,abc[j]} awkdemo2
aaa 2
bbbb 1
123 3
数值\字符串处理
1数值处理 rand()返回0和1之间一个随机数需有个种子 srand()没有种子一直输出0.237788
演示
[rootcentos111 test]# awk BEGIN{print rand()}
0.237788
[rootcentos111 test]# awk BEGIN{srand();print rand()}
0.973507
[rootcentos111 test]# awk BEGIN{srand();print rand()}
0.70811
---取0-50随机数
[rootcentos111 test]# awk BEGIN{srand();print int(rand()*100%50)1}
4
[rootcentos111 test]# awk BEGIN{srand();print int(rand()*100%50)1}
28
2字符串处理 length([s]) 返回指定字符串的长度 sub(r,s,[t]) 对t 字符串进行搜索r 表示的模式匹配的内容并将第一个匹配的内容替换为s gsub(r,s,[t]) 对t 字符串进行搜索r 表示的模式匹配的内容并全部替换为s 所表示的内容 plit(s,array,[r]) 以r 为分隔符切割字符串s 并将切割后的结果保存至array 所表示的数组中第一个索引值为1, 第二个索引值为2,…
演示
[rootcentos111 test]# echo 2008:08:08 08:08:08 | awk sub(/:/,-,$1)
2008-08:08 08:08:08
//搜索第一个值然后替换
[rootcentos111 test]# echo 2008:08:08 08:08:08 | awk gsub(/:/,-,$1)
2008-08-08 08:08:08
搜索所有的值进行替换
[rootcentos111 test]# echo 2008:08:08 08:08:08 | awk {split($0,i,:)}END{for(n in i){print n,i[n]}}
4 08
5 08
1 2008
2 08
3 08 08awk自定义函数
1格式和bash区别定义函数中需加参数return返回值不是$?是相当于echo输出
function name ( parameter, parameter, ... ) {statementsreturn expression
}
2演示
[rootcentos111 test]# cat fun.awk
function max(v1,v2) {v1v2?varv1:varv2return var
}
BEGIN{a3;b2;print max(a,b)}
[rootcentos111 test]# awk -f fun.awk
3awk中调用shell 命令
1system 命令 空格是awk 中的字符串连接符如果system中需要使用awk中的变量可以使用空格分隔或者说除了awk 的变量外其他一律用 引用 起来。
[rootcentos111 test]# awk BEGIN{system(hostname)}
centos1112向awk脚本传递参数
① 格式
awkfile varvalue var2value2... Inputfile
注意 在BEGIN 过程 中不可用。直到 首行输入完成以后变量才可用 。可以通过-v 参数让awk 在执行BEGIN 之前得到变量的值。命令行中每一个指定的变量都需要一个-v
② 示例
[rootcentos111 test]# cat test.awk
#!/bin/awk -f
{if($3 min $3max)print $1,$3}
[rootcentos111 test]# chmod x test.awk
[rootcentos111 test]# ./test.awk -F: min100 max200 /etc/passwd
systemd-network 192