AWK LEARN NOTE

AWK LEARN NOTE

参考教程及文档

  1. 阮一峰前辈的awk教程
  2. 左耳朵耗子叔awk教程
  3. sed and awk 101 hack

基本语法

语法形式

  awk -F ‘/pattern/{action}’ input-file
  
  #1. -F分界符字段,默认是空格
  #2. /pattern/是可选项,没有此项处理所有记录
  #3. action,所需要处理的命令,action必须在单引号且用大括号括起来
  #4. input-file:所需要处理的文本

awk字段

awk打印命令

image.png

如上图所示打印命令,$0代表当前行,所以awk将input.txt原样打印出来

awk会根据分界符将文本分成一个个字段,分别用$1,$2,$3等表示

打印第一个字段

image.png

打印第三个字段

image.png

带有pattern项的打印

image.png

格式化打印

image.png

awk内置变量

内置变量NF(number of field)

  • NF:当前行字段数量
  • $NF就是最后一个字段

NF

image.png

变量NR(number of row)

  • NR:当前处理的行号

NR不用加dollar符,加$变成字段

image.png

内置变量FS(field separator)

  • 输入字段分隔符
  • 效果等同于 -F ‘符号’

指定输入字段分隔符

指定输入字段分隔符为,文本中实际使用空格作为分隔符,所以会输出整行文本

image.png

内置变量OFS(output field separator)

  • OFS:输出字段分隔符

  • action指定

#以下三条awk语句的区别

awk '{print $1:$2}' input-file

awk '{print $1“:”$2}' input-file

awk '{print $1‘:’$2}' input-file

first awk

出现语法错误

image.png

second awk

image-20210307143336246.png

third awk

出现语法错误

image-20210307143520188.png

单引号和双引号在awk中的作用有很大不同

指定输出字段分隔符

awk 'BEGIN {OFS=";"} {print $2,$3}' input-file

image.png

内置变量RS(record separator)

  • RS:记录分隔符

image.png

记录分隔符RS应该理解为行与行之间的分隔,字段分隔符理解为行内各字符的分隔

记录分隔符

image.png

变量之间用分号分隔

image.png

内置变量ORS(output record separator)

  • 输出记录分隔符

image.png

内置变量FILENAME

  • 当前正在处理文件的文件名

image.png

awk变量

  • awk变量以字母开头,后续字符可以是数字、字母、下划线
  • awk变量不可以是关键字
  • awk变量不需要声明,可以直接使用
  • 变量初始化最好是在BEGIN区域,仅会执行一次

输出行范围

image.png

awk操作符

一元操作符列表

操作符 描述
+ 取正
- 取反
++ 自增
-- 自减

一元操作符+

image.png

一元操作符-

image.png

一元操作符++

pre自增

image.png

post自增

image.png

一元操作符--

pre自减

image.png

post自减

image.png

算数操作符

操作符 描述
+
-
*
/
% 取余

操作符+

image.png

操作符-

image.png

操作符*

image.png

操作符/

image-20210307231554526

操作符%

image.png

awk分支和循环

if else结构

#-----------------------#

if(condition){
	action1;
	action2;
}

#-----------------------#

if(condition1){
	action1;
} else{
	action2;
}

#-----------------------#

if(conditional-expression1)
	action1;
else if(conditional-expression2)
	action2;
else if(conditional-expression3)
	action3;
else
	action n;

image.png

while循环

while(condition) {
	action1;
	action2;
}

image.png

do-while循环

do{
	action1;
	action2;
}
while(condition)

for循环

for(initial_value;condition;incr/decr){
	command1;
	command2;
}

image.png

continue/break

  • continue:跳出当前循环,进入下一个循环
  • break:跳出整个循环

break

image.png

continue

image.png

exit

  • 立刻停止脚本运行

awk数据

数组元素

  • 数组元素的索引不必是连续的数字,也可以是字符串
  • 不需要指定数组长度
  • 数组索引没有顺序
arrayname[index]=value

数组元素赋值

card[1]="I"
card[2]="love"
card[3]="you"
card[you]="liana"

#打印数组元素
print arrayname[index]

image.png

image.png

循环遍历数组

for(index in arrayname){

	command1;
	command2;

}

image.png

删除数组元素

delete arrayname[index];

多维数组

awk实际上是不支持多维数组的,只是通过模拟的方式构建多维数组

#build 3x3 matrix
awk 'BEGIN {

card[1,1]=20
card[1,2]=40
card[1,3]=70
card[2,1]=55
card[2,2]=78
card[2,3]=96
card[3,1]=16
card[3,2]=54
card[3,3]=77

for(i=1;i<=3;i++){
    
    for(j=1;j<=3;j++){
    
        printf card[i,j] "  "
        
    }
    
    print
}

}'

image.png

SUBSEP下标分隔符

image.png

asort为数组元素排序

  • 数组索引将被重置为从1~n的索引值
  • assort(arrayname)将会返回重置后数组的个数

image.png

asorti为数组索引排序

  • assorti对数组索引进行排序
  • 排序后的数组原始元素值将丢失

image-20210311224221655.png

其他awk命令

格式化输出printf

字符 描述
换行符
水平制表符
v 垂直制表符
 退格
回车符
f 换页
字符 描述
s 字符串
c 单个字符
d 数值
e 指数
f 浮点数
g 使用e或f较短形式
o 八进制
x 十六进制
% 百分号

image.png

awk内置函数

int(n)函数

  • int函数返回给定参数整数部分
#!/usr/bin/bash

awk 'BEGIN {

        print "the value after int is "int(4)
        print "the value after int is "int(3.516)
        print "the value after int is "int(-5)
        print "the value after int is "int(-7.258)

}

'

image.png

log(n)函数

  • 返回i参数的自然对然
#!/usr/bin/bash

awk 'BEGIN {

        print "the value after int is "log(4)
        print "the value after int is "log(3.516)
        print "the value after int is "log(1)
        print "the value after int is "log(2.71828)

}
'

image.png

sqrt(n)函数

  • 返回参数的平方根
#!/usr/bin/bash

awk 'BEGIN {

        print "the value after int is "sqrt(4)
        print "the value after int is "sqrt(3.516)
        print "the value after int is "sqrt(1)
        print "the value after int is "sqrt(2.71828)

}

'

image.png

exp(n)函数

  • 返回e的n次幂
#!/usr/bin/bash

awk 'BEGIN {

        print "the value after int is "exp(0)
        print "the value after int is "exp(3)
        print "the value after int is "exp(1)
        print "the value after int is "exp(-1)

}

'

image.png

sin(n)/cos(n)/atan2(m,n)函数

  • 返回弧度值n的结果
#!/usr/bin/bash

awk 'BEGIN {

        print "the value after int is "sin(0)
        print "the value after int is "sin(3.1415926/6)
        print "the value after int is "sin(3.1415926/4)
        print "the value after int is "sin(3.1415926/2)

}

'

image.png

生成随机数rand()/srand(n)

  • rand()生成[0,1)之间的一个随机数值,包含0不包含1
  • srand(n)需要给定一个种子n,否则默认为当天日期时间作为种子,生成从n开始的随机数
awk ' BEGIN { 
	for(i=1;i<=10;i++) 
		print i " row generate value is " int(rand()*100) 
} '

image.png

awk字符串函数

index索引函数

  • index:输出字符串中指定字符的位置
  • 在string中检索pattern并返回位置
  • pattern需要使用double quote””
index(string,pattern)

awk ' BEGIN {
	name="ozyman dians"
    print "the position of di is ", index(name,"di")
} '

image.png

split分隔函数

  • split:字符串分隔成单个的数组元素
split(input-str,out-array,separator)  
#input-str:需要分隔的字符串 
#out-array:分隔后字符串存放的数组 
#separator:input-str分隔符

awk ' BEGIN {
	name="ozyman dians king of kings"
    split(name,name_array," ")
    for(i in name_array)
    	print "the name_array is " name_array[i]
} '

image.png

length字符串长度函数

  • length(string)返回输入字符串的的长度
length(string)  

awk ' BEGIN {
	name="ozyman dians king of kings"
    print "the string length of name is "length(name)
} '

image.png

substr字符串提取函数

  • 从输入字符串的指定位置提取指定长度的字符
substr(input-str,location,length)

awk ' BEGIN {
	name="ozyman dians king of kings"
    print substr(name,8)
    print substr(name,8,4)
} '

image.png

sub字符串替换函数

  • 替换第一次匹配的字符
  • 注意org-str和rep-str加双引号
  • input-str可选项,没有指定以$0作为input-str
  • gsub替换所有匹配的字符
sub(original-str,replace-str,input-str)

awk ' BEGIN {
	name="ozyman dians king of kings"
    sub("of","all other",name)
    print name
} '

image-20210317213019633.png

tolower/toupper

  • 将字符串转换为小写或大写
tolower(str) or toupper(str)

awk ' BEGIN {     
	name="Ozyman Dians king of kings"
    print name
    print tolower(name)
    print toupper(name)
} '

image.png

原文地址:https://www.cnblogs.com/movit/p/14483651.html