免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234
最近访问板块 发新帖
楼主: 落日大道
打印 上一主题 下一主题

求:一段小程序 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2005-01-14 16:41 |只看该作者

求:一段小程序

  1. #include <stdio.h>;
  2. #include <string.h>;
  3. #include <stdlib.h>;
  4. #define  USE_CMP "country: CN"

  5. int count_s = 0, sum_s = 0,block = 0;


  6. //读行函数start
  7. char * readline(FILE *frd)
  8. {
  9.         int i;

  10.         char *tmp, tmp_char,tmp_array[100];

  11.     int j = 0;

  12.         extern count_s, block, sum_s;

  13.        

  14.         while((tmp_char = fgetc(frd)) != '\n')
  15.         {
  16.                 if ( feof(frd)!=0 )
  17.                 {
  18.                         return NULL;
  19.                 }
  20.                
  21.                 tmp_array[j] = tmp_char;

  22.                 j++;
  23.        
  24.         }
  25.        
  26.         if (j==0)

  27.                 block = 1;
  28.        
  29.         count_s = j;


  30.         tmp = (char *)calloc(j,sizeof(char));

  31.         for (i = 0; i < j; i++ )

  32.                 *(tmp+i) = tmp_array[i];

  33.         return tmp;
  34. }
  35. //读行函数end


  36. //读块函数start
  37. char * readblock(FILE *fp)
  38. {
  39.         char *init, *tmp1, *tmp2;

  40.         int  count_i, sum_tmp;

  41.         extern sum_s, count_s, block;

  42.         while( block != 1 )
  43.         {
  44.                 sum_tmp = sum_s;//保存上一次的字符串长度!

  45.                 count_s = 0;//清零

  46.                 tmp1 = readline(fp);//读取下一行

  47.                 if (tmp1 != NULL)
  48.                 {
  49.                         sum_s += count_s;

  50.                         tmp2 = (char *)calloc(sum_s+1,sizeof(char));

  51.                         if ( sum_s != 0 )
  52.                         {
  53.                                 for(count_i = 0; count_i < sum_tmp;count_i++)//初始化原来的元素!

  54.                                         *(tmp2 + count_i) = *(init + count_i);

  55.                                 free(init);
  56.                         }
  57.                
  58.                         for(count_i = sum_tmp; count_i < count_s + sum_tmp;count_i++)

  59.                                 *(tmp2 + count_i) = *(tmp1 + count_i - sum_tmp);

  60.                         *(tmp2+count_i)='|';
  61.        
  62.                         init = (char *)calloc(sum_s+1,sizeof(char));

  63.                         for(count_i = 0; count_i < sum_s+1;count_i++)

  64.                                 *(init + count_i) = *(tmp2 + count_i);

  65.                         sum_s++;

  66.                         free(tmp1);

  67.                         free(tmp2);
  68.                 }
  69.                 else
  70.                         break;

  71.         };

  72.         tmp1 = (char *)calloc(sum_tmp-1,sizeof(char));

  73.         for(count_i = 0; count_i < sum_tmp-1;count_i++)

  74.                 *(tmp1 + count_i) = *(init + count_i);

  75.         *(tmp1 + count_i) = '\0';

  76.         free(init);

  77.         init = tmp1 ;

  78.         return init;
  79. }
  80. //读块函数end


  81. int compare_s(char *cmp)
  82. {
  83.         int i, start_c = 0, end_c = 0, j, cmp_stat = 0, len_cmp;

  84.         char *cmp_source;
  85.        
  86.         cmp_source = (char *)calloc(strlen(USE_CMP),sizeof(char));

  87.         strcpy(cmp_source,"country: CN");

  88.         len_cmp = strlen(cmp_source);

  89.         for (i = 0; i < sum_s; i++)
  90.         {
  91.                 if ( (*(cmp+i) == '|')&&(*(cmp+i+12) == '|') )

  92.                         for (j = 0; j < len_cmp; j++)
  93.                                
  94.                                 if ( *(cmp+j+i+1) != cmp_source[j] )
  95.                                 {
  96.                                         cmp_stat = 0;
  97.                                         break;
  98.                                 }
  99.                                 else
  100.                                 {
  101.                                         cmp_stat = 1;
  102.                                 }
  103.         }

  104.         return cmp_stat;

  105. }


  106. int main()
  107. {
  108.         FILE *fp,*wt;

  109.         int result, find_stat = 0, i;

  110.         char *tmp1;

  111.         extern count_s, sum_s, block;

  112.     fp = fopen("log1.dat","r");

  113.         wt = fopen("out.txt","w+");

  114.         while( feof(fp) == 0 )
  115.         {
  116.                 tmp1=readblock(fp);

  117.                 result = compare_s(tmp1);

  118.                 if (result==1)
  119.                 {
  120.                         find_stat++;

  121.                         for( i = 0; i < sum_s; i++)
  122.                         {

  123.                                 switch ( *(tmp1+i) )
  124.                                 {
  125.                                         case '|': fputc('\n',wt);break;

  126.                                         case '\0': fputc('\n',wt);break;

  127.                                         default: fputc( *(tmp1+i) ,wt);break;
  128.                                 }

  129.                         }
  130.                         fputc('\n',wt);
  131.                 }


  132.                 free(tmp1);

  133.                 block=sum_s=0;

  134.         }

  135.         fclose(fp);

  136.         printf("\n++++FIND RESULT: %d\n",find_stat);

  137.         return 0;
  138. }
复制代码

论坛徽章:
0
32 [报告]
发表于 2005-01-14 17:49 |只看该作者

求:一段小程序

楼上的用

code标签贴吧!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
33 [报告]
发表于 2005-01-14 18:00 |只看该作者

求:一段小程序

wingger法师的awk太牛啦

  1. awk 'BEGIN{RS=""}/country: CN/{print $0,"\n"}' file
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
34 [报告]
发表于 2005-01-14 18:52 |只看该作者

求:一段小程序

[quote]原帖由 "寂寞烈火"][/quote 发表:


我晕,不是我啊,呵呵,我可写不出来

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
35 [报告]
发表于 2005-01-14 19:09 |只看该作者

求:一段小程序

原帖由 "wingger" 发表:


我晕,不是我啊,呵呵,我可写不出来

法师别客气  
看来awk真的很强大呀
P.S:我写的那段,真是现眼  

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
36 [报告]
发表于 2005-01-23 09:29 |只看该作者

求:一段小程序

新抄来一段程序, 拿来 show 一下 :

  1. ${
  2.    /^$/!{
  3.       H
  4.       s/.*//
  5.    }
  6. }
  7. /^$/!{
  8.    H
  9.    d
  10. }
  11. /^$/{
  12.    x
  13.    /country: CN/p
  14.    d
  15. }
复制代码


把上面的内容存为文件 sedscr, 然后在命令行执行 " sed -f sedscr datafile " .
小弟试了一下. 好象没什么错(因为楼主给的东东太短了. 不知道会不会有问题).

另外, 楼主后来也没消息了. 不知道是不是搞定了. 要是搞定了. 小弟就成了画蛇添足了.  
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP