awk

awk是列编辑神器。我们经常听说,却敬而远之,但真的很简单很强大,我相信,你总会在某天需要它

一. 语法格式

 awk [ -F fs ] [ -v var=value ] [ 'prog' | -f progfile ] [ file ...  ]

例子:

awk '/sd/{print $1}' input.file

二. 参数解释

  • -v用于设置参数

搭建shell和awk之间的桥梁

A=kaka

awk -v a=$A 'BEGIN{print a} {print $1}' input.file
  • -F设置field seperator
awk -F ',' '{print $1}' input.file
  • -f指定prog脚本,下面解释
  • file列表awk要处理的文件对象

三. prog指令

awk执行一条条指令,叫做prog[ram],prog指令的格式如下,非常简单:

pattern { action }

包含pattern和action。 AWK按行读取数据,匹配pattern, 如果匹配,则执行对应的action

prog可以在命令行中给出,必须要用单引号”包含起来

或者写在文件中,通过-f引入

awk -f ./scripts input.file

多个prod

命令行中可以包含多个prog, 使用空格隔离

awk '/sd/{print $1} /2/{print$0}' input.file

3.1 pattern

awk按照pattern寻找匹配行,寻找到了匹配行之后,再对该行执行action。处理流程和sed一致。pattern可以省略,则意味着处理所有的行。pattern有多种匹配格式。

普通匹配

这种格式下,就是全文搜索,查找匹配pattern指定的正则表达式的行

awk '/p/{action}' 

范围匹配

这种格式是由两个patter组成,中间用逗号分隔。基本思路是规划一个区域,该域从满足p1的行开始,到满足p2的行结束

awk '/p1/,/p2/{action}'
  1. 会匹配包含p1,p2在内的之间所有行。
awk '/p1/,/p2/{print $0}' input.file
  1. 是循环匹配处理,如文件格式为
p1
xx
p2

p3

p1
xx
p2

则会跳过p3行不处理,上下两部分都处理

  1. 如果后半部分没有匹配成功,则全部处理.

其工作模式为,如果前半部分匹配,开始处理行,一直到匹配到后半部分,才会停止。
如文件格式为

xx
p1
xx
xx
awk '/p1/,/pt/{print $0}' input.file

则会打印从p1开始的所有行。

反向匹配

使用感叹号反相匹配

awk '!/pattern/{print $0}'

特殊pattern

有两个特殊的patter

  • BEGIN。匹配awk开始处理第一行之前
  • END。匹配awk处理完最后一行之后
awk -v a=$A 'BEGIN{print a} {print $1} END{print a}' input.file

3.2 action

action过于复杂,简直就是齐备的c语法,具体可以man awk,此处引用如下

if( expression ) statement [ else statement ]
while( expression ) statement
for( expression ; expression ; expression ) statement
for( var in array ) statement
do statement while( expression )
break
continue
{ [ statement ... ] }
expression              # commonly var = expression
print [ expression-list ] [ > expression ]
printf format [ , expression-list ] [ > expression ]
return [ expression ]
next                    # skip remaining patterns on this input line
nextfile                # skip rest of this file, open next, start at top
delete array[ expression ]# delete an array element
delete array            # delete all elements of array
exit [ expression ]     # exit immediately; status is expression

四. 内嵌函数和特殊变量

awk还包括内嵌函数和特殊变量,如:

length 
    the length of its argument taken as a string, or of $0 if no argument.

rand   
    random number on (0,1)

srand  
    sets seed for rand and returns the previous seed.

int    
    truncates to an integer value

substr(s, m, n)
      the n-character substring of s that begins at position m counted from 1.

index(s, t)
      the position in s where the string t occurs, or 0 if it does not.

此处不详细讨论

fields

基本上我们把awk当作列处理工具,虽然它能做的还会更多。但我们只讨论列处理功能。其中$1, $2,。。分别表示1列,2列。。, $0表示整列, $NF表示最后一列



《 “awk” 》 有 4 条评论

  1. There are also certain other risk factors that can increase the chance of developing kidney cancer get cheap cytotec without dr prescription But other than the acne, he hasn t really had any other side effects

  2. Clinician ratings indicated that only mild redness, swelling, or induration at the injection site was observed in subjects treated with 37 generic cytotec pill

  3. pastillas priligy en mexico It is believed that in the presence of higher inspired oxygen concentrations, free radical formation increases, accelerating lung injury

  4. priligy otc The majority 74 were age 70 years or older

回复 buy generic cytotec online 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About Me

一位程序员,会弹吉他,喜欢读诗。
有一颗感恩的心,一位美丽的妻子,两个可爱的女儿
mail: geraldlee0825@gmail.com
github: https://github.com/lisuxiaoqi
medium: https://medium.com/@geraldlee0825