GNU gawk工具的功能是将指定文件中符合指定模式(pattern)的行按指定的动作(action)进行格式化处理
语法:gawk [options] [program] [file-list]
1、program格式
'pattern {action}'
2、$1,...,$n: 第1列,...,第n列
3、~ (matches operator):包含
$ gawk '$1 ~ /h/' cars
4、关系运算符:==,<=,>=,!=,>,<
#dpkg –get-selections | gawk '$2 != "install" {print $0}'
$ gawk '$5 <= 3000' cars
5、范围运算符:,
$ gawk '/volvo/ , /bmw/' cars
6、BEGIN 模式、END模式
$ gawk 'END {print NR, "cars for sale." }' cars
7、$0: 当前行
8、length函数
$ gawk '{print length, $0}' cars | sort -n
9、NR (record number)变量
$ gawk 'length > 24 {print NR}' cars
$ gawk 'NR == 2 , NR == 4' cars
10、OFS变量:输出字段分隔符(onput field separator)
BEGIN {OFS = " "}
{
if ($1 ~ /ply/) $1 = "plymouth"
if ($1 ~ /chev/) $1 = "chevrolet"
print
}
11、FS: 输入字段分隔符(input field separator)
$ cat find_uid
BEGIN {FS = ":"
saveit = 0}
$3 > saveit {saveit = $3}
END {print "Next available UID is " saveit + 1}
$ gawk -f find_uid /etc/passwd
Next available UID is 192
$ gawk -F: '{print $2}' file; //-F选项用来在命令行上给FS重新赋值
$ gawk -F[: ] '{print $2}' file //使用多个分隔符:空格、冒号:和TAB
12、printf函数
BEGIN {
printf "%-10s %-8s %2d %5d $ %8.2f",
$1, $2, $3, $4, $5
}
13、重定向输出
$ cat redirect_out
/chevy/ {print > "chevfile"}
/ford/ {print > "fordfile"}
END {print "done."}
$ gawk -f redirect_out cars
14、if...else控制结构
$ cat price_range
{
if ($5 <= 5000) $5 = "inexpensive"
else if (5000 < $5 && $5 < 10000) $5 = "please ask"
else if (10000 <= $5) $5 = "expensive"
printf "%-10s %-8s %2d %5d %-12s",$1, $2, $3, $4, $5
}
15、getline
读取一行,然后gawk的指针指向下一行(NR递增)。如果不带变量:行指针立即变化,NR递增,$0-$n变化;如果带变量: 行指针在{}内不变化,NR递增,$0-$n不变化
$ cat g4
{
print NR, "$0:", $0
getline aa
print NR, "aa:", aa
}
$ gawk -f g4 < alpha
1 $0: aaaaaaaaa
2 aa: bbbbbbbbb
3 $0: ccccccccc
4 aa: ddddddddd
16、其它
gawk 的|&相当于bash的管道 |
gawk能够从网络上获取文件的信息
gawk中的变量赋值后,引用变量不用$,直接引用,不同于bash。
=-=-=-=-=
Powered by Blogilo