<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>中文 &#187; 黄析伟</title>
	<atom:link href="http://software.intel.com/zh-cn/blogs/author/xiwei-huang/feed/" rel="self" type="application/rss+xml" />
	<link>http://software.intel.com/zh-cn/blogs</link>
	<description></description>
	<lastBuildDate>Mon, 28 May 2012 14:23:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>脚本小工具 - 基于Txt的简单数据表查询</title>
		<link>http://software.intel.com/zh-cn/blogs/2008/08/25/txt/</link>
		<comments>http://software.intel.com/zh-cn/blogs/2008/08/25/txt/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 08:12:25 +0000</pubDate>
		<dc:creator>黄析伟</dc:creator>
				<category><![CDATA[其他]]></category>

		<guid isPermaLink="false">http://software.intel.com/zh-cn/blogs/2008/08/25/txt/</guid>
		<description><![CDATA[在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 #&#124; nodename     hostname       ostype    admin &#124; 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 其中#开始的行是注释, 会被忽略. #&#124;...&#124; 行定义的是字段名字 其他行为数据. 下面是简单的使用例子: [...]]]></description>
			<content:encoded><![CDATA[<p>在Linux下写脚本的时候, 通常需要根据某些字段对数据进行检索，也就是要实现一般数据库的select功能。在脚本里面这样的功能通常要求很简单， 数据结构也不太复杂， 不需要动用真正的数据库。基于txt格式的简单表是很好的选择，但仅仅使用bash并不太好实现查询， 一般需要通过perl，python来实现。 这里给了一个例子是使用awk来完成的，大家可以直接拿去使用。</p>
<p>两个文件：plaindb.awk是awk的代码, simple_select是bash的wrapper. 代码见后.</p>
<p>以下是定义的文本数据表的格式示例:</p>
<p>--- nodes.conf ------</p>
<p>#configure test nodes here<br />
#format: nodename IP/hostname OStype root/Admin<br />
#e.g.:   specjdriver 192.168.6.67 windows Administrator<br />
#| nodename     hostname       ostype    admin |<br />
specjdriver   192.168.6.67   windows   Administrator<br />
specjsut      192.168.6.252  linux     root<br />
specjdb       192.168.6.70   windows   Admin<br />
specjemulator 192.168.6.66   linux     root</p>
<p>其中#开始的行是注释, 会被忽略. #|...| 行定义的是字段名字 其他行为数据.</p>
<p>下面是简单的使用例子:</p>
<p>[root@rac1 config]# ./simple_select nodes.conf nodename=specjdb hostname<br />
192.168.6.70<br />
[root@rac1 config]# ./simple_select nodes.conf ostype=windows nodename<br />
specjdriver<br />
specjdb</p>
<p>在脚本中用来做查询非常方便:)</p>
<p>附代码:</p>
<p>plaindb.awk:</p>
<p>------------------------------</p>
<p>#variable predefined:<br />
# condition,columnids<br />
BEGIN{<br />
 # read condition<br />
 count = split(condition,condstrs,"=");<br />
 if(count != 2 )<br />
  exit -1;<br />
 keyid = condstrs[1];<br />
 keyval = condstrs[2];<br />
 # read schema<br />
 do{<br />
  res = getline;<br />
 }while(res != 0 &amp;&amp; $0 !~ /#\|.*\|/);<br />
 if(res ==0 ){<br />
#  print "no schema found!!"<br />
  exit -1;<br />
 }<br />
 count = split($0,schema);<br />
 colidcount = split(columnids,columnidarray,",")<br />
 for(i=1;i&lt;=colidcount;i++)<br />
  columnindexarray[i] = -1;<br />
 keyindex = -1;<br />
# printf "searched: |%s|%s|\n",keyid,columnid;<br />
 for(i=2;i&lt;count;i++){<br />
#  printf "|%s| - %d \n",schema[i], i;<br />
  if(schema[i] == keyid)<br />
   keyindex = i - 1;<br />
  for(j=1;j&lt;=colidcount;j++)<br />
   if(schema[i] == columnidarray[j])<br />
    columnindexarray[j] = i - 1;<br />
 }<br />
 if(keyindex == -1){<br />
    #  print "no column found!", keyindex,columnindex;<br />
  exit -1;<br />
 }<br />
 for(i=1;i&lt;=colidcount;i++)<br />
  if(columnindexarray[i]==-1)<br />
   exit -1;<br />
# print "Columns found!", keyindex,columnindex;<br />
 do{<br />
  res = getline row;<br />
  if( res == 0){<br />
#   print "reached the end";<br />
   exit -1;<br />
  }<br />
  count = split(row,columns);<br />
#  print count, ":", row;<br />
  if(columns[keyindex] == keyval){<br />
   for(i=1;i&lt;=colidcount;i++)<br />
    printf ("%s\t",columns[columnindexarray[i]]);<br />
   printf("\n");<br />
   continue;<br />
  }<br />
 }while(1);</p>
<p>  <br />
}<br />
/.*/{printf("");}</p>
<p>-----------------------------------------</p>
<p>simple_select:</p>
<p>-----------------------------------------</p>
<p>#!/bin/bash<br />
CONDITION=$2<br />
COL_ID=$3<br />
awk -v condition="$CONDITION" -v columnids=$COL_ID -f ./plaindb.awk $1</p>
<p>----------------------------------------------------------------</p>
]]></content:encoded>
			<wfw:commentRss>http://software.intel.com/zh-cn/blogs/2008/08/25/txt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

