欢迎大家赞助一杯啤酒🍺 我们准备了下酒菜:Formal mathematics/Isabelle/ML, Formal verification/Coq/ACL2, C++/F#/Lisp
AWK
您可以在Wikipedia上了解到此条目的英文信息 AWK Thanks, Wikipedia. |
AWK是Unix平台上一种可以对文本进行逐行处理的编程语言,它来源于3个创作者的名字:Aho、(Peter)Weinberg和(Brain)Kernighan. 与sed和grep很相似,awk是一种样式扫描与处理工具,但其功能却大大强于sed和grep。awk提供了极其强大的功能:它几乎可以完成grep和sed所能完成的全部工作,同时,它还可以可以进行样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数。awk的三位创建者已将它正式定义为:样式扫描和处理语言。
gawk 的主要功能是针对档案的每一行(line)搜寻指定的 patterns。当一行里有符合指定的 patterns,gawk 就会在此一行执行被指定的 actions。 gawk 依此方式处理输入档案的每一行直到输入档案结束。
gawk 程式是由很多的 pattern 与 action 所组成,action 写在大括号 { } 里面,一个pattern後面就跟著一个action。整个 gawk 程式会像下面的样子:
pattern {action} pattern {action}
gawk 常被用来处理数 GB 的日志文件
目录 |
GNU Awk
gawk - GNU awk, a pattern scanning and processing language
apt-get install gawk
Mawk
mawk - a pattern scanning and text processing language
apt-get install mawk
常见用法
gawk '{ sum += $1 }; END { print sum }' file awk '{ print }' /etc/passwd awk '{ print "" }' /etc/passwd awk '{ print "huihoo" }' /etc/passwd gawk -F: '{ print $1 }' /etc/passwd awk -F":" '{ print $1 $3 }' /etc/passwd awk -F":" '{ print $1 " " $3 }' /etc/passwd awk -F":" '{ print "username: " $1 "\t\tuid:" "$3" }' /etc/passwd awk 'END {print NR}' html.c // 获得 html.c文件的行数
Print first two fields in opposite order:
awk '{ print $2, $1 }' file
Print lines longer than 72 characters:
awk 'length > 72' file
Print length of string in 2nd column
awk '{print length($2)}' file
Print fields in reverse order:
awk '{ for (i = NF; i > 0; --i) print $i }' file
Print all lines between start/stop pairs:
awk '/start/, /stop/' file
Print column 3 if column 1 > column 2:
awk '$1 > $2 {print $3}' file
Print line if column 3 > column 2:
awk '$3 > $2' file
Count number of lines where col 3 > col 1
awk '$3 > $1 {print i + "1"; i++}' file
Print sequence number and then column 1 of file:
awk '{print NR, $1}' file
Print every line after erasing the 2nd field
awk '{$2 = ""; print}' file
Print hi 28 times
yes | head -28 | awk '{ print "hi" }'
Print hi.0010 to hi.0099 (NOTE IRAF USERS!)
yes | head -90 | awk '{printf("hi00%2.0f \n", NR+9)}'
Print out 4 random numbers between 0 and 1
yes | head -4 | awk '{print rand()}'
Print out 40 random integers modulo 5
yes | head -40 | awk '{print int(100*rand()) % 5}'