以下中文来自baidu:
AWK是一种优良的文本处理工具。它不仅是 Linux 中也是任何环境中现有的功能最强大的数据处理引擎之一。这种编程及数据操作语言 (其 名称得自于它的创始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首个字母)的最大功能取决于一个人所拥有的知识。 AWK 提供了极其强大的功能:可以进行样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数。它具备了一个完整的语言所应具有的几乎所有精美特 性。实际上 AWK 的确拥有自己的语言: AWK 程序设计语言 , 三位创建者已将它正式定义为“样式扫描和处理语言”。它允许您创建简短的程序,这些程序读取输入文件、为数据排序 、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。
最简单地说, AWK 是一种用于处理文本的编程语言工具。AWK 在很多方面类似于 shell 编程语言,尽管 AWK 具有完全属于其本身的语法。它的设计思想来源于 SNOBOL4 、sed 、Marc Rochkind设计的有效性语言、语言工具 yacc 和 lex ,当然还从 C 语言中获取了一些优秀的思想。在最初创造 AWK 时,其目的是用于文本处理,并且这种语言的基础是,只要在输入数据中有模式匹配,就执行一系列指令。该实用工具扫描文件中的每一行,查找与命令行中所给定 内容相匹配的模式。如果发现匹配内容,则进行下一个编程步骤。如果找不到匹配内容,则继续处理下一行。 尽管操作可能会很复杂,但命令的语法始终是: awk '{pattern + action}' 其中 pattern 表示 AWK 在数据中查找的内容,而 action 是在找到匹配内容时所执行的一系列命令。花括号 ({}) 不需要在程序中始终出现,但它们用于根据特定的模式对一系列指令进行分组。 gawk 是 AWK 的 GNU 版本。
1 one example:
先看一个文件:
netxu26:/test # cat inven
1. Pen 5 20.00
2. Pencil 10 2.00
3. Rubber 3 3.50
4. Cock 2 45.50
netxu26:/test # awk '{print $1 $2"-->RS." $3*$4}' inven
1.Pen-->RS.100
2.Pencil-->RS.20
3.Rubber-->RS.10.5
4.Cock-->RS.91
-->RS.0
#Don't confuse $1,$2 etc with command line arguments of shell script
awk program statement | Explanation |
'{ print $1 $2 "--> Rs." $3 * $4 } ' | print command is used to print contains of variables or text enclose in " text ". Here $1, $2, $3,$4 are all the special variable. $1, $2, etc all of the variable contains value of field. Finally we can directly do the calculation using $3 * $4 i.e. multiplication of third and fourth field in database. Note that "--> Rs." is string which is printed as its. |
Note $1,$2 etc (in awk) also know as predefined variable and can assign any value found in field.
netxu26:/test # awk '{print $0}' inven
1. Pen 5 20.00
2. Pencil 10 2.00
3. Rubber 3 3.50
4. Cock 2 45.50
$0 is special variable of awk , which print entire record
Predefined variable of awk
netxu26:/test # cat > def_var
{
print "Printing Rec. #" NR "(" $0 "),And # of field for this record is " NF
}
netxu26:/test # awk -f def_var inven
Printing Rec. #1(1. Pen 5 20.00),And # of field for this record is 4
Printing Rec. #2(2. Pencil 10 2.00),And # of field for this record is 4
Printing Rec. #3(3. Rubber 3 3.50),And # of field for this record is 4
Printing Rec. #4(4. Cock 2 45.50),And # of field for this record is 4
Printing Rec. #5(),And # of field for this record is 0
awk Variable | Meaning |
FILENAME | Name of current input file |
RS | Input record separator character (Default is new line) |
OFS | Output field separator string (Blank is default) |
ORS | Output record separator string (Default is new line) |
NF | Number of input record |
NR | Number of fields in input record |
OFMT | Output format of number |
FS | Field separator character (Blank & tab is default) |
利用awk进行算术运算:
netxu26:/test # cat > math
{
print $1 " + " $2 " = " $1 + $2
print $1 " - " $2 " = " $1 - $2
print $1 " / " $2 " = " $1 / $2
print $1 " x " $2 " = " $1 * $2
print $1 " mod " $2 " = " $1 % $2
}
netxu26:/test #
netxu26:/test #
netxu26:/test # awk -f math
20 3
20 + 3 = 23
20 - 3 = 17
20 / 3 = 6.66667
20 x 3 = 60
20 mod 3 = 2
AWK BEGIN...END:
Use BEGIN pattern to set value of variables, to print heading for report etc. General syntax of BEGIN is as follows
Syntax:
BEGIN {
action 1
action 2
action N
}
END instruct awk, that perform END actions after reading all lines (RECORD) from the database file. General syntax of END is as follows:
END {
action 1
action 2
action N
}
netxu26:/test # cat > bill2
BEGIN {
print "---------------------------"
print "Bill for the 4-March-2001. "
print "By Vivek G Gite. "
print "---------------------------"
}
{
total = $3 * $4
recno = $1
item = $2
gtotal += total
print recno item " Rs." total
}
END {
print "---------------------------"
print "Total Rs." gtotal
print "==========================="
}netxu26:/test #
netxu26:/test # awk -f bill2 inven
---------------------------
Bill for the 4-March-2001.
By Vivek G Gite.
---------------------------
1.Pen Rs.100
2.Pencil Rs.20
3.Rubber Rs.10.5
4.Cock Rs.91
Rs.0
---------------------------
Total Rs.221.5
===========================
netxu26:/test #
awk中的printf:
Syntax:
printf "format" ,var1, var2, var N
Format Specification Code | Meaning | Example |
%c | Character | { isminor = "y" printf "%c" , isminor } |
%d | Decimal number such as 10,-5 etc | { n = 10 printf "%d",n } |
%x | Hexadecimal number such as 0xA, 0xffff etc | { n = 10 printf "%x",n } |
%s | String such as "vivek", "Good buy" | { str1 = "Welcome to Linux!" printf "%s", str1 printf "%s", "Can print ?" } |