1 / 20

AWK

AWK. 11 级 ACM 班 郑辉煌 5110209289. 简介. 1970s . Bell labs Alfred Aho , Peter Weinberger , Brain Kernighan 样式扫描处理语言 处理文本的编程语言工具 来源于 SNOBOL4 , Bourne shell , C 可以进行正则表达式的匹配,样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数. AWK 语法. awk [ -F re] [parameter...] [' prog '] [- f progfile ][ in_file ...]

emera
Télécharger la présentation

AWK

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AWK 11级 ACM班 郑辉煌 5110209289

  2. 简介 • 1970s. Bell labs • Alfred Aho,Peter Weinberger,Brain Kernighan • 样式扫描处理语言 • 处理文本的编程语言工具 • 来源于SNOBOL4,Bourne shell, C • 可以进行正则表达式的匹配,样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数

  3. AWK语法 • awk[ -F re] [parameter...] ['prog'] [-f progfile][in_file...] • -F re: • parameter: • 'prog': 标准形式为: 'pattern {action}' • -f progfile: • in_file

  4. AWK程序结构 • 输入数据 模式匹配 • 每次扫描一行(记录)…域 • 发现匹配内容执行动作 • 未发现匹配内容下一行

  5. AWK ‘prog’ • pattern { action } • pattern表示AWK在数据中查找的内容 • 而action 是在找到匹配内容时所执行的一系列命令。 • 无pattern默认匹配全部的记录 • 而无 action则是打印原始记录。

  6. Example • BEGIN { print “Hello, world!” } #hello world • length($0) > 80 • { w += NF #单词数 • c += length + 1 #字符数 • } • END { print NR, w, c }#NR是记录条数

  7. Example • $awk '/sun/{print}' mydoc • $awk ‘/sun/’ mydoc #简化 • $awk '/[Ss]un/,/[Mm]oon/ {print}' myfile • 第一个匹配Sun或sun的行与第一个匹配Moon或moon的行之间的行,并显示到标准输出上

  8. awk记录与字段(域) • 在缺省的情况下:将文本文件中的一行视为一个记录。 • awk借用shell 的方法,用$1,$2,$3...这样的方式来顺序地表示记录中的不同字段。 • 特殊地,awk用$0表示整个行(记录)。 • 不同的字段之间是用称作分隔 符的字符分隔开的。系统默认的分隔符是空格。 • awk允许在命令行中用-F re的形式来改变这个分隔符。

  9. 内置变量 • NR: 已输入记录的条数。 • NF: 当前记录中域的个数。记录中最后一个域可以以$NF的方式引用。 • FILENAME: 当前输入文件的文件名。 • FS: “域分隔符”,用于将输入记录分割成域。其默认值为“空白字符”,即空格和制表符。FS可以替换为其它字符,从而改变域分隔符。 • RS: 当前的“记录分隔符”。默认状态下,输入的每行都被作为一个记录,因此默认记录分隔符是换行符。 • OFS: “输出域分隔符”,即分隔print命令的参数的符号。其默认值为空格。 • ORS: “输出记录分隔符”,即每个print命令之间的符号。其默认值为换行符。 • OFMT: “输出数字格式”(Format for numeric output),其默认值为"%.6g"。

  10. Example • awk -F % 'NR==7,NR==15 {printf $1 $3 $7}‘ myfile • 显示文本文件myfile中第七行到第十五行中以字符%分隔的第一字段,第三字段和第七字段:

  11. Example • { s += $NF } # $NF是该行最后一个域 • END { print s + 0 } • 计算最后一个单词和 • 注意s + 0这句

  12. 与C类似 • if…else • while • for • break, continue • || , >=, +=……不同:~匹配,!~不匹配 • 函数:printf, getline,sin,exp • …其他内置函数

  13. Example •  $awk '{printf"%03d%s\n",NR,$1}' testAwk • { while(getline == 1)#成功1,失败0 • { • … • } • }

  14. 更复杂的:计算词频 • BEGIN { FS="[^a-zA-Z]+" } • { • for (i=1; i<=NF; i++) • words[tolower($i)]++ #tolower返回小写 • } • END { • for (i in words) #与很多语言不同的特殊用法 • print i, words[i] • }

  15. 自定义函数 • # 示例函数 必须有关键字function • function add_three (number) { return number + 3 } print add_three(36) # 输出 39

  16. Example • awk>‘BEGIN{pageno = 1; file = FILENAME> pageno = print_header(file,pageno);> printf("当前页页号是:%d\n", pageno); >} >#定义函数print_header>function print_header(FileName, PageNum){ > printf("%s %d\n",FileName, PageNum); > PageNum++; return PageNUm;  > } >}' myfile myfile1 当前页页号是:2 

  17. 高级输入输出 • 1.读取下一条记录:next语句读取下一个记录并完成模式匹配,然后立即执行相应的操作。 next导致这个记录的任何额外匹配模式被忽 略。 • 2.关闭文件(输入,输出文件) • close (”filename”)

  18. 3.文件输出 • awk中允许用如下方式将结果输出到一个文件: • printf("hello word!\n") > "datafile" 或printf("hello word!\n") >> "datafile"  • 重定向到管道 • print "expression" | "command"

  19. Reference • Wiki • 各种网络资源 • Q and A

  20. Thanks

More Related