终止gawk程序,使用Ctrl+D组合键产生一个EOF终止符。
[root@localhost ~]# gawk '{print "Hello World"}'
this is a test
Hello World
you need input ctrl+D to end the shell
Hello World
数据字段变量,默认分隔符是任意的空白字符(例如空格或制表符)
$0代表整个文本。
$1代表文本中的第1个数据字段
$2代表文本中的第2个数据字段
$n代表文本中的第n个数据字段
字段提取
[root@localhost shell]# cat helloworld.txt
one line of this file
two line of this file
three line of this file
four line of this file
five line of this file
[root@localhost shell]# gawk '{print $1}' helloworld.txt
one
two
three
four
five
-F分隔符
[root@localhost shell]# gawk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
BEGIN打印之前执行脚本
[root@localhost shell]# cat helloworld.txt
Line 1
Line 2
Line 3
[root@localhost shell]# gawk 'BEGIN {print "The helloworld.txt File Contents:"};{print $0}' helloworld.txt
The helloworld.txt File Contents:
Line 1
Line 2
Line 3
在处理数据后运行脚本
[root@localhost shell]# gawk 'BEGIN {print "The helloworld.txt File Contents:"};{print $0}; END {print "The end."}' helloworld.txt
The helloworld.txt File Contents:
Line 1
Line 2
Line 3
The end.