目录
1.awk的工作流程
2.awk程序的执行方式
3.awk打印一个内容和打印多个内容
4.awk中所有内置变量的使用,以及自定义变量并使用
5.awk执行数学计算:
6.awk的模式的使用:
7. awk中控制语句
8.awk中内置函数的使用:
AWK 工作流程可分为三个部分:
格式化输出:显示Hello World字符串且宽度为50,向左对齐
[root@localhost test]# echo "hello world"|awk -F "\n" '{printf "%-50s\n", $1}'
-F "指定分割符"
%s: 显示字符
+ 左对齐
- 右对齐
number 宽度
[root@localhost test]# echo "a b c"|awk '{print $0}'
a b c
[root@localhost test]# echo "a b c"|awk '{print $1,$3}'
a c
[root@localhost test]# echo "a b c"|awk '{print NF}'
3
[root@localhost test]# cat file.text
hello beijing
hello tianjing
hello hebei
export TERM=xterm
[root@localhost test]# awk '{print NR}' file.text
1
2
3
4
[root@localhost test]# awk '{print NR}' file.text file_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost test]# awk '{print FNR}' file.text file_test
1
2
3
4
1
2
3
4
5
6
7
8
9
10[root@localhost test]# awk '{print FILENAME}' file.text
file.text
file.text
file.text
file.text
[root@localhost test]# echo "a b:c d-e:f"|awk -F ":" '{print $1}'
a b
[root@localhost test]# echo "a b:c d-e:f"|awk -F ":" '{print $2}'
c d-e[root@localhost test]# echo "a b:c d-e:f"|awk -F ":" 'BEGIN{OFS="#"}{print $1,$2,$3}'
a b#c d-e#f[root@localhost test]# echo "a b:c d-e:f"|awk -F ":" 'BEGIN{RS="-"}{print $1,$2,$3}'
a b c d
e f
[root@localhost test]# awk '{print ENVIRON["PATH"]}' file.text
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
10/2*3+5%2+2^3
[root@localhost test]# awk '{print 10/2*3+5%2+2^3}'
24
awk处理文本:要求文本有5行内容,且当行数为奇数的时候打印第一个字段
[root@localhost test]# vim dispose_text.sh
{
m=NR
if ( m%2 == 1 ){
print $1
}
}
[root@localhost test]# ./dispose_text.sh file.text
hello
hello
this
awk处理文本: 要求文本有5行内容, 当行数不为3时打印第一个字段
[root@localhost test]# vim dispose_text.sh
{
m=NR
if ( m != 3 ){
print $1
}
}
[root@localhost test]# ./dispose_text.sh file.text
hello
hello
export
this
awk处理文本:文本内容为ls -l /root的内容,匹配所有的普通文件的文件名
[root@localhost test]# ls -l /root|awk '{++f[$NF]}{for(i in f);j=i+1;print "test -f /root/$j"|"bash"}'
每个模式下加样例
[root@localhost test]# awk 'BEGIN{print}' result
[root@localhost test]# awk '{print}' result
result
[root@localhost test]# awk 'BEGIN{k=4}{print k}' result
4
END模式
[root@localhost test]# awk '{k=1}END{print k;k=3}{print k}' result
1
1
BEGINFILE
ENDFILE
/regular expression/
[root@localhost test]# awk '/^hello/ {print}' test_text
hello china 1
hello chongqing 2
hello shaanxixian 3
hello guandong 4 hello
hello zhejiang 5
relational expression
[root@localhost test]# awk '$1 >= 70 {print}' number.text
70
88
101
pattern && pattern
[root@localhost test]# awk '/^zhangsan/ && $2>= 70 {print}' name_grad.text
zhangsan 71
pattern || pattern
[root@localhost test]# awk '/^zhangsan/ || $2>= 70 {print}' name_grad.text
zhangsan 66
wangwu 90
zhangsan 71
lisi 98
wangwu 88
pattern ? pattern : pattern
[root@localhost test]# awk '2>1?/^zhangsan/:/^lisi/ {print}' name_grad.text
zhangsan 66
zhangsan 71
[root@localhost test]# awk '2<1?/^zhangsan/:/^lisi/ {print}' name_grad.text
lisi 55
lisi 98
(pattern)
[root@localhost test]# awk '(/^zhangsan/) {print}' name_grad.text
zhangsan 66
zhangsan 71
[root@localhost test]# awk '(/88/) {print}' name_grad.text
wangwu 88
! pattern
[root@localhost test]# awk '!/^zhangsan/ {print}' name_grad.text
lisi 55
wangwu 90
lisi 98
wangwu 88
pattern1, pattern2
[root@localhost test]# more name_grad.text
zhangsan 66
lisi 55
wangwu 90
zhangsan 71
lisi 98
wangwu 88
[root@localhost test]# awk '/^zhangsan/ {print}' name_grad.text
zhangsan 66
zhangsan 71
[root@localhost test]# awk '/^zhangsan/,/^lisi/ {print}' name_grad.text
zhangsan 66
lisi 55
zhangsan 71
lisi 98
if: 给定一个成绩0-100,输出等级: A:85-100, B:70-84, C:60-69, D:0-59
[root@localhost test]# vim compute.sh
{
if ( $1 >= 85) {
print "your grades are:A"
}
else{
if ( $1 >= 70 ) {
print "your grades are:B"
}
else {
if ( $1 >=60 ) {
print "your grades are:C"
}
else {
print "your grades are:D"
}
}
}
}
[root@localhost test]# ./compute.sh number.text
your grades are:D
your grades are:C
your grades are:B
your grades are:A
your grades are:A
for(): 计算1+2...+100的和
[root@localhost test]# vim for_accumulate.sh
{
for (i=1;i<=100;i++)
{
result=result+i
}
print result
}
[root@localhost test]# ./for_accumulate.sh
5050
for(in): 定义一个数组:数组中的元素为: array[name]=age,-> zhangsan:18 lisi:20 wangwu=21
循环访问数组,并输出数组中的key和value
[root@localhost test]# vim array.sh
{
array["zhangsan"]=18
array["lisi"]=20
array["wangwu"]=21
for (i in array) {
print i,array[i]
}
}
[root@localhost test]# ./array.sh result
zhangsan 18
wangwu 21
lisi 20
用while和do...while实现9*9乘法表
[root@localhost test]# vim while_9x9.sh
{
i=1
while (i<=9)
{
j=1
while (j<=i)
{
printf "%-8s", i"*"j"=" i*j
j++
}
i++
print " "
}
[root@localhost test]# ./while_9x9.sh result
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
substr
[root@localhost test]# awk '{print substr("abcdef",2,3)}' result
bcd
tolower
[root@localhost test]# awk '{print tolower("WWw.HAha.Com")}' result
www.haha.co
toupper
[root@localhost test]# awk '{print toupper("WWw.HAha.Com")}' result
WWW.HAHA.COM
system
[root@localhost test]# awk '{print system(hostname)}' result
0
[root@localhost test]# awk '{print system($PATH)}' result
sh: result: command not found
127
game over