结果这样的啊。
叶子改一个读入过程中输出的吧。我比较下。逐行读入的。
代码: 全选
#!/bin/python
import re
r=r'ID.*?ICON'
d=open("a.txt").read()
g=re.findall(r,d,re.S)
for a in g:
r=r'MAIL=(.*?)\n.*?PHONE=(.*?)\n'
x=re.search(r,a,re.S)
if x :
m,p=(x.group(1).strip(),x.group(2).strip())
if m and p:
print "%s,%s"%(m,p)
代码: 全选
#!/bin/python
d=open("a.txt")
e=False
m=p=None
for a in d:
if a.find('ICON')==0: e=True
if a.find('MAIL')==0: m=a[5:].strip()
if a.find('PHONE')==0: p=a[6:].strip()
if e and m and p:
print "%s,%s"%(m,p)
e=False
m=p=None
代码: 全选
import sys
c = sys.stdin.read()
for i in c.split('#CONTACT'):
d = {}
for j in i.split('\n'):
if j.find('=') >= 0:
key, value = j.split('=', 1)
d[key] = value
if d.has_key('NAME') and d.has_key('PHONE'):
sys.stdout.write('%s,%s\n' % (d['NAME'], d['PHONE']))
在上面已经贴出。eexpress 写了:这样ok。有空改逐行读入的吧。
我稍微改了下,你检查是是不是 ICON/MAIL/PHONE 的大小写问题?eexpress 写了:额。第2个没输出。球猫的也没。球猫的是管道吧,没输出。文件作参数,也是“死机”。
代码: 全选
#!/bin/sh
#ALL Variables
cleaner(){
NAME=
PHONE=
#需要补全,我偷个懒好吧!只清空关键字段。
}
writer(){
[ -z "$NAME" ]||[ -z "$PHONE" ]&&return ##其中有一个关键字段空,就不输出。
echo ${NAME},$PHONE >> b.txt
}
while read LINE;do
case "$LINE" in
"")
continue
;;
\#CONTACT)
writer
cleaner
;;
*)
VAR=${LINE%%=*}
VUL=${LINE##*=}
if [ "$VAR" = "NAME" ];then
NAME="$VUL"
elif [ "$VAR" = "PHONE" ];then
PHONE="$VUL"
fi
;;
esac
done < $1
writer #保证最后一组数据写入
检查你的文件里面是不是这几个参数是不是都是大写的?你要name?ok.eexpress 写了:额。叶子怎么检查icon mail phone啊。奇怪了。我是去name phone
不是大小写的事情。
代码: 全选
#!/bin/python
import re
r=r'CONTACT.*?ICON'
d=open("a.txt").read()
g=re.findall(r,d,re.S)
for a in g:
r=r'NAME=(.*?)\n.*?PHONE=(.*?)\n'
x=re.search(r,a,re.S)
if x :
n,p=(x.group(1).strip(),x.group(2).strip())
if n and p:
print "%s,%s"%(n,p)
代码: 全选
#!/bin/python
d=open("a.txt")
e=False
n=p=None
for a in d:
if a.find('NAME')==0:
n=a[5:].strip()
e=True
if a.find('PHONE')==0: p=a[6:].strip()
if e and n and p:
print "%s,%s"%(n,p)
e=False
n=p=None
eexpress 写了:额。第2个没输出。球猫的也没。球猫的是管道吧,没输出。文件作参数,也是“死机”。
代码: 全选
cat b |python a.py
binsos@googlegroups.com,1xxxxx999901
代码: 全选
● cat t.pl
#!/usr/bin/perl
while(<>){
chomp;
$name=$',next if /NAME=/;
print $name.",".$'."\n" if /PHONE=/ && $';
}