脚本小工具 - 基于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
其中#开始的行是注释, 会被忽略. #|...| 行定义的是字段名字 其他行为数据.
下面是简单的使用例子:
[root@rac1 config]# ./simple_select nodes.conf nodename=specjdb hostname
192.168.6.70
[root@rac1 config]# ./simple_select nodes.conf ostype=windows nodename
specjdriver
specjdb
在脚本中用来做查询非常方便:)
附代码:
plaindb.awk:
------------------------------
#variable predefined:
# condition,columnids
BEGIN{
 # read condition
 count = split(condition,condstrs,"=");
 if(count != 2 )
  exit -1;
 keyid = condstrs[1];
 keyval = condstrs[2];
 # read schema
 do{
  res = getline;
 }while(res != 0 && $0 !~ /#\|.*\|/);
 if(res ==0 ){
#  print "no schema found!!"
  exit -1;
 }
 count = split($0,schema);
 colidcount = split(columnids,columnidarray,",")
 for(i=1;i<=colidcount;i++)
  columnindexarray[i] = -1;
 keyindex = -1;
# printf "searched: |%s|%s|\n",keyid,columnid;
 for(i=2;i<count;i++){
#  printf "|%s| - %d \n",schema[i], i;
  if(schema[i] == keyid)
   keyindex = i - 1;
  for(j=1;j<=colidcount;j++)
   if(schema[i] == columnidarray[j])
    columnindexarray[j] = i - 1;
 }
 if(keyindex == -1){
    #  print "no column found!", keyindex,columnindex;
  exit -1;
 }
 for(i=1;i<=colidcount;i++)
  if(columnindexarray[i]==-1)
   exit -1;
# print "Columns found!", keyindex,columnindex;
 do{
  res = getline row;
  if( res == 0){
#   print "reached the end";
   exit -1;
  }
  count = split(row,columns);
#  print count, ":", row;
  if(columns[keyindex] == keyval){
   for(i=1;i<=colidcount;i++)
    printf ("%s\t",columns[columnindexarray[i]]);
   printf("\n");
   continue;
  }
 }while(1);
  
}
/.*/{printf("");}
-----------------------------------------
simple_select:
-----------------------------------------
#!/bin/bash
CONDITION=$2
COL_ID=$3
awk -v condition="$CONDITION" -v columnids=$COL_ID -f ./plaindb.awk $1

本文转自Intel_ISN 51CTO博客,原文链接:http://blog.51cto.com/intelisn/130725,如需转载请自行联系原作者
相关文章
|
1月前
|
监控 数据处理 索引
使用Python批量实现文件夹下所有Excel文件的第二张表合并
使用Python和pandas批量合并文件夹中所有Excel文件的第二张表,通过os库遍历文件,pandas的read_excel读取表,concat函数合并数据。主要步骤包括:1) 遍历获取Excel文件,2) 读取第二张表,3) 合并所有表格,最后将结果保存为新的Excel文件。注意文件路径、表格结构一致性及异常处理。可扩展为动态指定合并表、优化性能、日志记录等功能。适合数据处理初学者提升自动化处理技能。
23 1
|
6月前
动态将用户指定的内表的内容通过 Excel 导出
动态将用户指定的内表的内容通过 Excel 导出
38 0
|
安全 API 数据安全/隐私保护
|
SQL Python
python读取错误excel记录生成sql,避免手工维护数据
python读取错误excel记录生成sql,避免手工维护数据
python读取错误excel记录生成sql,避免手工维护数据
|
存储 应用服务中间件 BI
浅谈导出Execl的报表数据解决方案
项目中免不了要做一些数据导出功能,比如导出前一天的订单记录。涉及导出功能可大可小,根据系统具体的用户量、一天的订单量等情况。今天就讨论一下怎么设计一个合理的导出功能。
117 0
浅谈导出Execl的报表数据解决方案
|
索引
[20180503]视图提示使用索引.txt
[20180503]视图提示使用索引.txt --//昨天优化sql语句,想提示某个视图里面的表使用索引,有点忘记ZALBB以前讲过的提示写法,看了以前链接, --//自己在写一个例子便于记忆.
936 0
|
关系型数据库 MySQL 数据库
|
关系型数据库 Shell 数据库