脚本小工具 - 基于Txt的简单数据表查询
在Linux下写脚本的时候, 通常需要根据某些字段对数据进行检索,也就是要实现一般数据库的select功能。在脚本里面这样的功能通常要求很简单, 数据结构也不太复杂, 不需要动用真正的数据库。基于txt格式的简单表是很好的选择,但仅仅使用bash并不太好实现查询, 一般需要通过perl,python来实现。 这里给了一个例子是使用awk来完成的,大家可以直接拿去使用。
两个文件:plaindb.awk是awk的代码, simple_select是bash的wrapper. 代码见后.
以下是定义的文本数据表的格式示例:
--- nodes.conf ------
#configure test nodes here
#format: nodename IP/hostname OStype root/Admin
#e.g.: specjdriver 192.168.6.67 windows Administrator
#| nodename hostname ostype admin |
specjdriver 192.168.6.67 windows Administrator
specjsut 192.168.6.252 linux root
specjdb 192.168.6.70 windows Admin
specjemulator 192.168.6.66 linux root
其中#开始的行是注释, 会被忽略. #|...| 行定义的是字段名字 其他行为数据.