- 论坛徽章:
- 0
|
需要解析一个文件(pnote_file),格式如下.
"SECTION:<str>{ " 和 "}END"分别做为起始和结束的关键字.
读取文件的每一个行,如果是section里面的内容,把它显示出来.
我觉得用perl没有问题, 每次用正则表达式判断当前str的内容.
再用一个flag来标识起始和结束就ok了.
在parse_pnote() 我写了大概的perl.
但是, 需要用shell ( #!/bin/sh ).
我对shell还不是很熟, 请大家指点一下.
多谢!
================================================
pnote_file:
SECTION:1{
This is section 1 infor
}END
SECTION:2{
This is section 2 infor
}END
===========================================
#!/bin/sh
flag=0
infor_str=""
#sub function
parse_pnote()
{
line_str=$*
echo "The current str is : $line_str"
if [ $line_str=~/SECTION(.*){(\s*)$/ ] && (flag=0);then
flag=1
else
if [$line_str=~/SECTION(.*){(\s*)$/ && (flag=1)];then
#出错 上一个SECTION没有END匹配
else
#记录信息 infor_str
fi
fi
if [ $line_str=~/}END/ && (flag=1)]
flag=0
echo "the infor is $infor_str"
infor_str=""
fi
}
#main
while read LINE ; do
parse_pnote $LINE;
done < $pnote_file |
|