从文件中提取符合要求的数据,编程语言不限,最好是perl/python/shell/vb/c其中的一种,采纳后另附送30分

有一文件,其格式为: 共3列,第1列是方位(north, west...),第2列是坐标,第3列是判定条件(共4个,A,B,C和D),(各列之间以tab隔开,且数据按照sort -k1 -k2排过序)。示例如下所示:

01 east 10088649 B
02 east 10088650 B
03 east 10088712 C
04 north 10088738 B
05 north 10088739 D
06 north 10088857 C
07 north 10088898 A
08 north 10088899 A
09 north 10188900 B
10 south 10189041 A
11 south 11089042 B
12 south 11089043 A
13 south 11089162 D
14 south 11089163 C
15 west 10089218 A
16 west 10089309 A
17 west 10089539 A
18 west 10089581 A
19 west 10089582 B
20 west 10089649 D
最终的输出要求是: 以第2列为基准,在同一方位内,提取区间范围为2000以内,且同时包含A,B,C,D 4个判定条件的行。

比如说,在上边的例子中,4-8行以及11-14行就是符合要求的。各位大侠谁能帮忙编个程序实现此目的?多谢

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(void)
{
FILE *fp;
int i=0,j=0,l=0,m=0,second=0,snd=0,sec21=0,sec22=0,sec23=0;
char buf[10],first[10],str[10],t,orien[10];
char sb31[10],sb32[10],sb33[10],third[10],pthird[10];

if ((fp=fopen("xuan.txt","r"))==NULL)
{
printf("read err!\n");
exit(0);
}

memset(buf,0x00,10);
while (!feof(fp))
{
fscanf(fp,"%c",&t);

if (t!='\t' && t!='\n')
{
buf[i]=t;
i++;
}
else
{
i=0;
j++;
if (j==1)
{
strcpy(first,buf);
}
if (j==2)
{
second=atoi(buf);
}
if (j==3)
{
strcpy(third,buf);
}
memset(buf,0x00,10);

if (j==3)
{
l++;
j=0;
m++;
if (abs(second-snd)<2000 && strcmp(orien,first)==0)
{
if (m==1 && (strcmp(third,pthird)!=0))
{
strcpy(sb31,pthird);strcpy(sb32,third);sec21=snd;sec22=second;
}
else if (m==2 && strcmp(third,sb31)!=0 && strcmp(third,sb32)!=0)
{
strcpy(sb33,third);sec23=second;
}
else if (m==3 && strcmp(third,sb31)!=0 && strcmp(third,sb32)!=0 && strcmp(third,sb33)!=0)
{
printf(orien);printf("\t");itoa((l-3),str,10);printf(str);printf("---");itoa(l,str,10);printf(str);printf("\n");
strcpy(sb31,sb32);strcpy(sb32,sb33);strcpy(sb33,third);snd=sec21;sec21=sec22;sec22=sec23;sec23=second;
}
else if (m>3 && strcmp(third,sb31)!=0 && strcmp(third,sb32)!=0 && strcmp(third,sb33)!=0)
{
printf(orien);printf("\t");itoa((l-3),str,10);printf(str);printf("---");itoa(l,str,10);printf(str);printf("\n");
strcpy(sb31,sb32);strcpy(sb32,sb33);strcpy(sb33,third);snd=sec21;sec21=sec22;sec22=sec23;sec23=second;
}
else
{
m=0;
snd=second;
strcpy(orien,first);
}
}
else
{
m=0;
snd=second;
strcpy(orien,first);
}
strcpy(pthird,third);
}
}
}
fclose(fp);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-30
有没有可能这样
01 east 1 A
02 east 2 B
03 east 3 C
04 east 4 D
05 east 2001 A
这样的话 01~04 和 02~05 都提取出来?