欢迎大家赞助一杯啤酒🍺 我们准备了下酒菜:Formal mathematics/Isabelle/ML, Formal verification/Coq/ACL2/Agda, C++/Lisp/Haskell
AWK
第11行: | 第11行: | ||
==常见用法== | ==常见用法== | ||
gawk '{ sum += $1 }; END { print sum }' file | 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 | 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}' | ||
+ | |||
==Links== | ==Links== | ||
*AWK用法简介 - http://clayboy1.blogchina.com/clayboy1/3461065.html | *AWK用法简介 - http://clayboy1.blogchina.com/clayboy1/3461065.html |
2007年2月1日 (四) 21:32的版本
AWK是Unix平台上一种可以对文本进行逐行处理的编程语言,它来源于3个创作者的名字:Aho、(Peter)Weinberg和(Brain)Kernighan. 与sed和grep很相似,awk是一种样式扫描与处理工具,但其功能却大大强于sed和grep。awk提供了极其强大的功能:它几乎可以完成grep和sed所能完成的全部工作,同时,它还可以可以进行样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数。awk的三位创建者已将它正式定义为:样式扫描和处理语言。
目录 |
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}'