Skip to content

Latest commit

 

History

History
168 lines (121 loc) · 4.01 KB

Chapter2.The-AWK-Language.md

File metadata and controls

168 lines (121 loc) · 4.01 KB

2.1 Patterns

BEGIN and END

  • BEGIN { statements }
  • END { statements }

前面已经讲过BEGIN和END分别匹配第一行之前和最后一行之后。 一般BEGIN用于初始化变量,修改内置变量。 BEGIN、END可以出现多次。按出现次数依次执行。

在做实验的时候可以将所有代码放到BEGIN的action里:

$ awk '
BEGIN {
     a = "abc def"
     b = gensub(/(.+) (.+)/, "\\2 \\1", "g", a)
     print b
}'
def abc

Expressions as Patterns

expression { statements }

String-Matching Patterns

Regular Expressions

  • /regexp/ { statements }
  • expression ~ /regexp/ { statements }
  • expression !~ /regexp/ { statements }

Compound Patterns

compound pattern { statements }

Range Patterns

pattern1, pattern2 { statements }

$ awk 'NR==2,NR==4{print}' emp.data
Dan     3.75    0
Kathy   4.00    10
Mark    5.00    20

Summary of Patterns

2.2 Actions

  • expression, with constants, variables, assignments, functions, etc
  • print expression-list
  • printf(format, expression-list)
  • if (expression) statement
  • if (expression) statement else statement
  • while (expression) statement
  • for (expression; expression; expression) statement
  • for (variable in array) statement
  • do statement while epxression
  • break
  • continue
  • next
  • exit
  • exit expression

Expressions

Control-Flow Statements

Empty Statement

Arrays

2.3 User-Defined Functions

2.4 Output

The print statement

两种书写形式

$ awk '{print expr1, expr2, ... , exprn}' # 无括号
$ awk '{print(expr1, expr2, ... , exprn)}' # 有括号

打印空行

$ awk '{print ""}'

Output Separators

  • OFS = The Output Field Separator, a space by default
  • ORS = The Output Record Separator, by default a newline
$ awk 'BEGIN{OFS="->";ORS=";\n"};{print($1, $2)}' emp.data
Beth->4.00;
Dan->3.75;
Kathy->4.00;
Mark->5.00;
Mary->5.50;
Susie->4.25;

可以通过man awk可以查看内置变量。

2.5 Input

$ awk 'program' data
$ egrep 'Asia' countries | awk 'program'

Input Separators

$ awk 'BEGIN{ FS="|" }' data
$ awk -F'|' 'program' data

Multiline Records

The getline Function

Explicit Input with getline

getline的形式 说明 解释
getline Using getline with no arguments 将下一行读到列变量$0,$1,...$n里。相当于读了两行
getiine var Using getline into a variable 将下一行读到某个变量var。相当于读了两行
getline < "file" Using getline from a file 从指定文件读取一行到$0
getline var "file" Using getline into a variable from a file 从指定文件读取一行放到变量var中
cmd | getline Using getline from a pipe 从命令输出读取一行到$0(行尾有换行)
cmd | getline var Using getline into a variable from a pipe 从命令输出读取一行到变量var(行尾有换行)
print "some query" |& "db_server"
"db_server" |& getline Using getline from a coprocess 双向管道,输出读到$0
cmd |& getline var Using getline into a variable from a coprocess 双向管道,输出读到变量var

getline Summary

Variant Effect awk / gawk
getline Sets $0, NF, FNR, NR, and RT awk
getline var Sets var, FNR, NR, and RT awk
getline < file Sets $0, NF, and RT awk
getline var < file Sets var and RT awk
command | getline Sets $0, NF, and RT awk
command | getline var Sets var and RT awk
command |& getline Sets $0, NF, and RT gawk
command |& getline var Sets var and RT gawk

注意: 有Pipe的要用close(cmd)关掉对应管道

Command-Line Variable Assigments

Command-Line Arguments

2.6 Interaction with Other Programs

2.7 Summary