还是看例子:
$ gawk 'BEGIN { print "This is the key word BEGIN" } { print $0}' data
This is the key word BEGIN
No.1 Google Gmail
No.2 Microsoft Windows
No.3 SAP ERP
No.4 Intel Core
No.5 Cisco Router
在打印data中的文本前先输出了BEGIN中的内容。下面再看看END是啥效果:
$ gawk 'BEGIN { print "This is the key word BEGIN" } { print $0} END { print "This is the key word END"}' data
This is the key word BEGIN
No.1 Google Gmail
No.2 Microsoft Windows
No.3 SAP ERP
No.4 Intel Core
No.5 Cisco Router
This is the key word END
很简单是吧,最后呢,再体验一下gawk的威力,你就回觉得这家伙真心牛逼。
新建脚本文件script
BEGIN {
print "The latest list of users and shells"
print "Userid shell "
print "---------------------------------------------"
FS=":"
}
{
print $1 "\t\t\t" $7
}
END {
print "---------------------------------------------"
print "This is the end of the list"
}
输出结果:
$ gawk -f script /etc/passwd
The latest list of users and shells
Userid shell
---------------------------------------------
root /bin/bash
daemon /usr/sbin/nologin
bin /usr/sbin/nologin
sys /usr/sbin/nologin
sync /bin/sync
games /usr/sbin/nologin
man /usr/sbin/nologin
lp /usr/sbin/nologin
mail /usr/sbin/nologin
news /usr/sbin/nologin
uucp /usr/sbin/nologin
proxy /usr/sbin/nologin
www-data /usr/sbin/nologin
backup /usr/sbin/nologin
list /usr/sbin/nologin
irc /usr/sbin/nologin
gnats /usr/sbin/nologin
nobody /usr/sbin/nologin
systemd-timesync /bin/false
systemd-network /bin/false
systemd-resolve /bin/false
systemd-bus-proxy /bin/false
syslog /bin/false
_apt /bin/false
messagebus /bin/false
uuidd /bin/false
lightdm /bin/false
whoopsie /bin/false
avahi-autoipd /bin/false
avahi /bin/false
dnsmasq /bin/false
colord /bin/false
speech-dispatcher /bin/false
hplip /bin/false
kernoops /bin/false
pulse /bin/false
rtkit /bin/false
saned /bin/false
usbmux /bin/false
comac /bin/bash
sshd /usr/sbin/nologin
---------------------------------------------
This is the end of the list
生成这么清晰明了的报表,就写到这里吧。
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx