shell 脚本最常见的用途之一是处理文本文件.
检查日志文件, 读取配置文件, shell 脚本可以帮助我们将一下日常处理任务自动化. sed
和 gawk
工具可以极大简化文本数据处理任务.
sed
是 流编辑器(stream editor).
普通的交互式文本编辑器(如vim): 用键盘命令交互地插入, 删除, 替换文本;
流编辑器: 在编辑器处理数据基于 预先提供的一组规则.
sed
虽然功能强大, 但是今天我们不介绍它.
因为我们要介绍一个更强大的 gawk
.
1 gawk 简介
gawk
是 Unix 中原始的 awk
程序的 GNU 版本.
我的 Linux 系统里的 awk
命令是通过 软链接 指向 gawk
的:
$ type awk
awk is /usr/bin/awk
$ type gawk
gawk is /usr/bin/gawk
$ ll /usr/bin/awk
lrwxrwxrwx. 1 root root 4 Jun 22 2020 /usr/bin/awk -> gawk
gawk
程序让 流编辑 迈上了一个新的台阶, 因为 gawk
提供了一种编程语言而不是 流编辑命令.
gawk
命令的基本格式:
gawk options program file
gawk
会针对 数据流 的 每行文本 执行程序脚本(program).
在命令行中, program 必须包在 ''
中.
而行 数据处理 的 program 需要放在花括号 {}
中, 所以 命令行中数据处理程序 可以写成 '{program}'
.
数据处理 之前的 program 放在 BEGIN {}
的花括号中, 数据处理 之后的 program 放在 END {}
的花括号中.
gawk
的 可选项(options) :
选项 | 描述 |
---|---|
-F fs | field-separator 指定行中划分数据字段的 字段分隔符 |
-f file | 从指定的文件中读取程序 |
-v var=value | 定义 gawk 程序中的一个变量及其默认值 |
-mf N | 指定文件中的最大字段数 |
-mr N | 指定文件中最大数据行数 |
-W keyword | 指定gawk 的兼容模式 或 警告等级 |
gawk
的强大之处在于 程序脚本(program), 可以写脚本来处理文本行数据.
gawk
脚本程序 用 一对花括号 {}
来定义. 必须把 脚本命令 放到 花括号{}
中.
看个示例:
$ gawk '{print "hello world"}'
123
hello world
456
hello world
====
hello world
^C
上面的例子中, 不论在命令行输入啥, gawk
总是打印一句 “hello world”.
print
命令用于将文本打印到 STDOUT. 由于例子中没有在 命令行 指定文件名, 所以 gawk
会从 STDIN 接收数据. 本例中, gawk
会一直等待从 STDIN 输入.
2 使用数据字段变量
$0
代表整个文本行.
$1
代表第 1 个数据字段.
$2
代表第 2 个数据字段.
$n
代表第 n 个数据字段.
gawk
读取文件中的第一个字段 $1
:
$ cat test.txt
one line of test text.
two line of test text.
three line of test text.
$ gawk '{print $1}' test.txt
one
two
three
使用 -F
选项指定 字段分隔符 :
$ gawk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
...
如果 program 有多条命令, 命令之间可以用 分号(;
) 分隔, 也可以直接换行:
$ echo "my name is miyan" | gawk '{$4="rosie"; print $0}'
my name is rosie
$ echo "my name is miyan" | gawk '{$4="rosie"
pipe quote> print $0}'
my name is rosie
上面的 pipe quote> 是命令行中的 次提示符, 提示命令输入更多的数据, 直到输入了结尾的 单引号('
) 才会结束.
3 从文件中读取程序program
从 文件中读取 gawk
的 program, 只需要使用 -f file_name
即可:
$ cat t.gawk
{
print $1 "'s home directory is " $6
}
$ gawk -F: -f t.gawk /etc/passwd
root's home directory is /root
bin's home directory is /bin
daemon's home directory is /sbin
adm's home directory is /var/adm
...
gawk program 文件中使用变量值 不需要 像 shell 脚本一样加 $
:
$ cat t.gawk
{
text = "'s home directory is "
print $1 text $6
}
$ gawk -F: -f t.gawk /etc/passwd
root's home directory is /root
bin's home directory is /bin
daemon's home directory is /sbin
adm's home directory is /var/adm
...
4 在处理数据前/后运行脚本
BEGIN
关键字可以强制 gawk 在读取数据前, 先执行 BEGIN
关键字后面的 program:
$ gawk 'BEGIN {print "hello world"}'
hello world
$ cat test.txt
one line of test text.
two line of test text.
three line of test text.
$ gawk 'BEGIN {print "file contents: "} {print $0}' test.txt
file contents:
one line of test text.
two line of test text.
three line of test text.
与 BEGIN
类似, END
关键字会使 gawk
在读取完数据以后执行 END
关键字后的 program:
$ gawk 'BEGIN {print "file contents: "}
quote> {print $0}
quote> END {print "end of file"}' test.txt
file contents:
one line of test text.
two line of test text.
three line of test text.
end of file
Reference
[1]. Linux命令行与shell脚本编程大全(第三版)