AWK

来自开放百科 - 灰狐
(版本间的差异)
跳转到: 导航, 搜索
(Links)
 
(未显示1个用户的8个中间版本)
第1行: 第1行:
 +
{{top news}}
 +
{{SeeWikipedia}}
 +
 
AWK是Unix平台上一种可以对文本进行逐行处理的编程语言,它来源于3个创作者的名字:Aho、(Peter)Weinberg和(Brain)Kernighan. 与[[sed]]和[[grep]]很相似,awk是一种样式扫描与处理工具,但其功能却大大强于sed和grep。awk提供了极其强大的功能:它几乎可以完成grep和sed所能完成的全部工作,同时,它还可以可以进行样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数。awk的三位创建者已将它正式定义为:样式扫描和处理语言。
 
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==
 
==GNU Awk==
 
gawk - GNU awk, a pattern scanning and processing language
 
gawk - GNU awk, a pattern scanning and processing language
第8行: 第18行:
 
mawk - a pattern scanning and text processing language
 
mawk - a pattern scanning and text processing language
 
  apt-get install mawk
 
  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}'
  
 
==Links==
 
==Links==
第14行: 第63行:
 
*[http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-2/index.html 通用线程:Awk 实例,第 2部分 - 记录、循环和数组]
 
*[http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-2/index.html 通用线程:Awk 实例,第 2部分 - 记录、循环和数组]
 
*[http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-3/index.html 通用线程:Awk 实例,第 3部分 - 字符串函数和……支票簿?]
 
*[http://www-128.ibm.com/developerworks/cn/linux/shell/awk/awk-3/index.html 通用线程:Awk 实例,第 3部分 - 字符串函数和……支票簿?]
 +
 +
[[category:programming language]]
 +
[[category:Awk]]

2016年2月29日 (一) 12:57的最后版本

Wikipedia-35x35.png 您可以在Wikipedia上了解到此条目的英文信息 AWK Thanks, Wikipedia.

AWK是Unix平台上一种可以对文本进行逐行处理的编程语言,它来源于3个创作者的名字:Aho、(Peter)Weinberg和(Brain)Kernighan. 与sedgrep很相似,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}'

[编辑] Links

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱