shell 按行读取文件,涉及读配置文件
2012-08-12 23:30
316 查看
logfile=/tmp/test.log datafile=datal words1=^^suc^^ words2= words3= numfile=nu
#读config 配置文件 while read line; do eval "$line" done <config #配置项赋值 numberfile=$numfile keyword=$words1 logfile=$logfile datafile=$datafile #创建记录行号的文件 touch $numberfile #读取上一次的行号 a=`cat $numberfile` line=0 #读取本次行号 newline=`wc -l $datafile | awk '{print $1}'` #比较上一次($numberfile中的数字)与本次的行号 #本次行号增加,从上一次的位置开始读 if [ $a -lt $newline ];then line=$a fi #行号相等,退出程序 if [ $a -eq $newline ];then exit fi #行号减少,从0开始读 if [ $a -gt $newline ];then line=0 fi #匹配关键字的特殊字符处理 word=`echo $keyword | sed -e 's/\^/\\\^/g'` #匹配过程 b=`awk'NR>'$line'{if($0!~/'\$word'/) {if($0!~/^[[:space:]]*$/){print "["$0"]"}}}' $datafile` #屏幕打印的消息 echo $b | awk '{if($0!~/^\s*$/) {print "出现不正常:"$0} else {print "判断时段内正常"}}' #写入此次的行号,下次用 newline=`wc -l $datafile | awk '{print $1}'` echo $newline>$numberfile
注意linux hp-ux平台对正则匹配$的情况
($0!~/'\$word'/注意-gt -lt -eq对数值的比较,不用<>=
if [ $a -gt $newline ]注意$line 引用awk外部命令用单引号
'NR>'$line'注意赋值的时,不是$line=$a
line=$a注意[[:space:]] = \s
($0!~/^[[:space:]]*$/)
该脚本在实际运行过程当中,遇到 awk 处理行超过3000字节数的警告。
修改如下:
# echo $b | awk '{if($0!~/^\s*$/) {print "出现不正常:"$0} else {print "判断时段内正常"}}'
if [ `echo '$b' | grep "^\s*$"` ];then echo "该监控时段内未出现异常!" else echo "产生异常,异常信息是:""$b" fi
输出结果为多行:
aaaa
bbbb
cccc
要使结果多行变一行:
echo "产生异常,异常信息是:""$b" | xargs
转载著名出处,尊重作者No. Liu
相关文章推荐
- shell脚本按行读取配置文件并解析
- Shell脚本读取ini配置文件的实现代码2例
- SHELL读取 ini 格式文件做配置文件
- shell脚本读取配置文件
- shell脚本按行读取文件并解析
- shell脚本读取ini配置文件的值
- Shell登录类型,以及读取配置文件的顺序
- shell编程学习之道:shell按行读取文件的方法
- shell读取nginx配置文件中nginx的端口
- shell脚本读取ini配置文件的值
- shell脚本读取配置文件
- Shell实现读取ini格式配置文件方法
- shell脚本读取ini配置文件的值
- shell 按行读取文件的问题
- shell脚本读取ini配置文件的值
- Shell脚本——按行读取文件
- 基于ini配置文件实现多维数组数据的按行存储和读取
- shell脚本读取配置文件
- shell脚本按行读取文件并解析
- shell脚本按行读取文件并解析