awk的工作流程

首先,awk的基本语法为 awk ‘pattern{actions}’,其中pattern代表匹配模式,actions表示要执行的操作,pattern和actions都是可选的,但是两者必须至少有一个; 工作流程:(反复执行4个步骤) 1、自动从指定的数据文件中读取行文本 2、自动更新awk的内置系统变量的值,例如列数变量NF、行数变量NR、行变量$0以及各个列变量$1、$2等 3、依次执行程序中所有的匹配模式及其操作 4、当执行完程序中所有的匹配模式及其操作之后,如果数据文件中仍然还有未读取的数据行,则返回到第1步,重复执行1-4的操作。

awk程序的执行方式

1、通过命令执行方式awk程序

awk 'program-text' datafile

2、执行awk脚本

awk -f program-file file ...

program-file表示awk脚本文件名称,file代表要处理的数据文件 3、可执行脚本文件 需要再awk程序中指定命令解释器,并且赋予脚本可执行权限 指定命令解释器:#!/bin/awk -f 赋予可执行权限:chmod a+x file 最终可以通过以下命令执行:

awk-script file

#awk-script为awk脚本文件名称,file要处理的数据文件

awk打印一个内容和打印多个内容

格式化输出:显示Hello World字符串且宽度为50,向左对齐

[root@servera example]# vim hello.txt

Hello World

# 左对齐

[root@servera example]# awk '{printf "%-50s",$0}' hello.txt

Hello World [root@servera example]#

# 默认右对齐

[root@servera example]# awk '{printf "%50s",$0}' hello.txt

Hello World[root@servera example]#

awk中所有内置变量的使用,以及自定义变量并使用

awk内置变量

[root@servera example]# cat hello.txt

Hello World

hello cat

hello xiaoming

hello xiaohong

[root@servera example]#

$n 字段变量,其中n为整数,且n大于1。表示第n个字段的值

[root@servera ~]# echo "1:2:3:4" | awk -F : '{print $3}'

3

[root@servera ~]# echo -e "1:2:3:4\n11:22:33:44" | awk -F : '{print $4}'

4

44

NR 整数值,表示awk已经读入的记录数;如果有多个文件,这个数目会把处理的多个;文件中行统一计数。(显示的是文件的每一行的行号)

[root@servera example]# awk '{print NR}' hello.txt

1

2

3

4

NF 整数值,表示当前记录(变量$0所代表的记录)的字段数

[root@servera example]# awk '{print NF}' hello.txt

2

2

2

2

与NR不同的是,FNR用于记录正处理的行是当前这一文件中被总共处理的行数

[root@servera example]# awk '{print FNR}' hello.txt

1

2

3

4

$0 记录变量,表示当前正在处理的记录

[root@servera ~]# echo "1:2:3:4" | awk -F : '{print $0}'

1:2:3:4

FS 输入字段分隔符,默认值是空格或者制表符,可使用-F指定分隔符

[root@servera bash]# echo "1:2:3" | awk 'BEGIN{FS=":"}{print $0,$1,$2}'

1:2:3 1 2

OFS 输出字段分隔符 ,OFS=”#”指定输出分割符为#

[root@servera bash]# echo "1:2:3" | awk 'BEGIN{FS=":";OFS="-"}{print $0,$1,$2}'

1:2:3-1-2

ENVIRON 当前shell环境变量及其值的关联数组

[root@servera bash]# echo "" | awk '{print ENVIRON["PATH"]}'

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

awk自定义变量

[root@servera bash]# awk 'BEGIN{test="hello";print test}'

hello

[root@servera bash]# echo "" | awk 'BEGIN{test="hello";print test} test=="hello" {print test} END{print test}'

hello

hello

hello

[root@servera bash]# echo "" | awk -v test="hello" 'BEGIN{print test} test=="hello" {print test} END{print test}'

hello

hello

hello

awk文本处理,数学计算

awk执行数学计算: 10/2*3+5%2+2^3

[root@servera example]# awk 'BEGIN{print 10/2*3+5%2+2^3}'

24

awk处理文本:要求文本有5行内容,且当行数为奇数的时候打印第一个字段

[root@servera example]# cat hello.txt

one Hello World

two hello cat

three hello xiaoming

four hello xiaohong

five hello xiaozhang

[root@servera example]# awk '{if (NR%2==1 && "END{print NR}==5") print $1}' hello.txt

one

three

five

awk处理文本: 要求文本有5行内容, 当行数不为3时打印第一个字段

[root@servera example]# awk '{if (NR!=3 && "END{print NR}==5") print $1}' hello.txt

one

two

four

five

awk中控制语句

if: 给定一个成绩0-100,输出等级: A:85-100, B:70-84, C:60-69, D:0-59

[root@servera example]# cat score.txt

120

30

56

75

95

81

[root@servera example]# cat awk_if.sh

#!/bin/awk -f

{

if ($1 >= 85 && $1 <= 100){

print $1,"A"

}

else

{

if ($1 >= 70 && $1<=84){

print $1,"B"

}

else

{

if ($1 >= 60 && $1 <=69){

print $1,"C"

}

else

{

if ($1 >=0 && $1 <= 59){

print $1,"D"

}

else

{

print $1 " Tips:value 0-100"

}

}

}

}

}

[root@servera example]# chmod a+x awk_if.sh

[root@servera example]# ./awk_if.sh score.txt

120 Tips:value 0-100

30 D

56 D

75 B

95 A

81 B

for(): 计算1+2…+100的和

[root@servera example]# vim awk_for.sh

#!/bin/awk -f

BEGIN{

sum=0

for(i=1;i<=100;i++){

sum+=i

}

print "1+2+....+100="sum

}

[root@servera example]# ./awk_for.sh

1+2+....+100=5050

简单数组

for(in): 定义一个数组:数组中的元素为: array[name]=age,-> zhangsan:18 lisi:20 wangwu=21 循环访问数组,并输出数组中的key和value

[root@servera example]# vim awk_array.sh

#!/bin/awk -f

BEGIN{

array["zhangsan"]=18

array["lisi"]=20

array["wangwu"]=21

for(var in array){

print var,array[var]

}

}

[root@servera example]# ./awk_array.sh

zhangsan 18

wangwu 21

lisi 20

用while和do…while实现9*9乘法表

awk的while实现

[root@servera example]# vim while_awk.sh

#!/bin/awk -f

BEGIN{

j=1

while (i<=9){

while (j<=i){

t=i*j

printf("%d*%d=%d\t",i,j,t)

j++

}

i++

j=1

printf("\n")

}

}

[root@servera example]# chmod a+x while_awk.sh

[root@servera example]# ./while_awk.sh

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

awk的do while实现

[root@servera example]# vim do_while_awk.sh

#!/bin/awk -f

BEGIN {

i=1

do

{

j=1

do

{

printf("%d*%d=%d\t",i,j,i*j)

j++

}while(j<=i)

i++

printf("\n")

}while (i<=9)

}

[root@servera example]# chmod a+x do_while_awk.sh

[root@servera example]# ./do_while_awk.sh

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

awk中内置函数的使用:substr, tolower, toupper, system

[root@servera example]# vim awk_built_in_function.sh

#!/bin/awk -f

BEGIN{

print "------------------substr------------------"

str="hello world!"

new_str=substr(str,2,6)

printf("%s\n",new_str)

print "---------------------tolower,toupper--------------------"

a="love is story"

printf("a->A:%s\n",toupper(a))

A="LOVE IS STORY"

printf("A->a:%s\n",tolower(A))

print "---------------------system-----------------------"

print system("whoami")

}

[root@servera example]# chmod a+x awk_built_in_function.sh

[root@servera example]# ./awk_built_in_function.sh

------------------substr------------------

ello w

---------------------tolower,toupper--------------------

a->A:LOVE IS STORY

A->a:love is story

---------------------system-----------------------

root

0 #返回的状态码

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: