过滤多个文件中带warn的行,将该行保留到新文件中

来源:互联网 发布:故乡的云 知乎 编辑:程序博客网 时间:2024/06/06 13:20
#include <stdio.h>#include <stdlib.h>#include <string.h>// time#include <time.h>// mkdir#include <sys/stat.h>#include <sys/types.h>#include <dirent.h>// getcwd#include <unistd.h>// define macro#define TMP_FILE "temp.txt"#define MYDIR "backup_file"// define host number#define UGC_COUNT 30L#define ENUM_COUNT 3L#define SHIPINMENHU_COUNT 30L#define SANGMENHU_COUNT 23L#define NEIRONGGUANLI_COUNT 14L#define SHIPINIVR_COUNT 8L#define ZHWG_COUNT 4L/*Version 1.02011-3-16 add testserver check2011-5-9 add 4a host check2011-7-15 add move file to year dir2011-9-3 add move file to year dir for updateMsn: xorg@163.comFunction: handle xunjian file*/// define check_dir funtionvoid check_dir(char *);void move_file(char *,char *);//add 2011-09-03 检测目录是否存在,不存在则创建void check_dir(char path[512]) {int n=0;int ret;DIR *dp;char *dir,full_dir[512];char dup_path[512];char mkdir_cmd[128];memset(full_dir,0,sizeof(full_dir));memset(dup_path,0,sizeof(dup_path));setenv("LANG","zh_CN.GBK",1);      printf("path=%s\n",path);// copy duplication to opreationstrcpy(dup_path,path);for(dir=strtok(dup_path,"/");dir;dir=strtok(NULL,"/")) {printf("for dir=%s\n",dir);n++;if(n==1) {strcat(full_dir,dir);} else {strcat(full_dir,"/");strcat(full_dir,dir);}printf("for full_dir=%s\n",full_dir);dp=opendir(full_dir);printf("dp=%s\n",dp);if(dp==NULL) {sprintf(mkdir_cmd,"mkdir %s",full_dir);//system(mkdir_cmd);ret=system(mkdir_cmd);if(ret==-1) {fprintf(stderr,"system() error.");closedir(dp);exit(-1);}// setenv设置环境变量后,用mkdir函数创建的目录还是乱码,暂时只能用mkdir命令实现/*ret=mkdir(full_dir,0755);if(ret!=0) {if(errno==ENOENT) {printf("pathname does not exist\n");close(dp);exit(-1);}else {fprintf(stderr,"mkdir() other error.\n");close(dp);exit(-1);}}*/closedir(dp);}elseclosedir(dp);}}//add 2011-09-03 移动文件到指定的目录中void move_file(char *filename,char *backdir) {int ret;char cmd[128];// 调用check_dir函数,检测目录check_dir(backdir);// move commandsprintf(cmd,"mv %s %s >/dev/null 2>&1",filename,backdir);printf("cmd=%s\n",cmd);// execute commandret=system(cmd);if(ret==-1) {fprintf(stderr,"system() error.\n");exit(-1);}}// define today_file_handle function//void today_file_handle(char *array[],int COUNT,char *filename) {void today_file_handle(char *array[],int COUNT) {DIR *dp_ret1,*dp_ret2;struct dirent *ptr;struct tm *p1;time_t timep;int a=0,i,j,k,m,n=0,ret,line,q;FILE *fp_src,*fp_dst,*fp_tmp;char cmd1[512],*p,*str;char cmd2[512];char cmd3[512];char today_date[512];char today_date_4a[512];char today_dir[512],chk_dir[512];char year[512];char dst_file[128];char readbuf[1024];// 2011-10-09//char *dup_file;timep=time(NULL);p1=localtime(&timep);sprintf(dst_file,"%d-%02d-%02d-%02d-%02d-%02d.txt",(1900+p1->tm_year),(1+p1->tm_mon),(p1->tm_mday),(p1->tm_hour),p1->tm_min,p1->tm_sec);sprintf(today_date,"%02d%02d",(1+p1->tm_mon),(p1->tm_mday));sprintf(today_date_4a,"%d-%02d-%02d",(1900+p1->tm_year),(1+p1->tm_mon),(p1->tm_mday));sprintf(today_dir,"%d-%02d-%02d",(1900+p1->tm_year),(1+p1->tm_mon),p1->tm_mday);sprintf(chk_dir,"%s/%s",MYDIR,today_dir);sprintf(year,"%s%d","CST ",1900+p1->tm_year);//printf("%s\n",today_date);backdiryear// open dst_filefp_dst=fopen(dst_file,"w+");if(fp_dst==NULL) {fprintf(stderr,"fopen fp_dst error.\n");exit(-1);}// open TMP_FILEfp_tmp=fopen(TMP_FILE,"w+");if(fp_tmp==NULL) {fprintf(stderr,"fopen tmp file error.\n");exit(-1);}// open current directory        dp_ret2=opendir("./");        if(dp_ret2==NULL) {                fprintf(stderr,"opendir error.\n");                exit(-1);        }for(i=0;i<COUNT;i++) {m=0;while((ptr=readdir(dp_ret2))!=NULL) {if(strstr(ptr->d_name,array[i]) && strstr(ptr->d_name,today_date) || strstr(ptr->d_name,array[i]) && strstr(ptr->d_name,today_date_4a) != NULL) {m=1;//dup_file=ptr->d_name;//q=0;break;}}if(m==1) {//RPTDB02/*if(strstr(ptr->d_name,"RPTDB02")!=NULL)fprintf(fp_tmp,"%s\n",array[i]);*///fp_src=fopen(dup_file,"r");fp_src=fopen(ptr->d_name,"r");if(fp_src==NULL) {fprintf(stderr,"fopen %s error.\n",cmd1);exit(-1);//fprintf(fp_tmp,"warning!!! %s is down!!!!\n",array[i]);}line=0;while(fgets(readbuf,1024,fp_src) != NULL) {line++;fprintf(fp_dst,"%s",readbuf);//shipinmenhu host RPTDB02//if(line==1 && *readbuf!='\n')//if(array[i]!="zz-aq-4a-4" || array[i]!="zz-aq-4a-3" && line==1 && *readbuf!='\n')if(line==1 && *readbuf!='\n' && *readbuf==' ')fprintf(fp_tmp,"%s\n",array[i]);if(strstr(readbuf,"hostname") || strstr(readbuf,"主机名") || strstr(readbuf,year) != NULL)fprintf(fp_tmp,"%s",readbuf);if(strstr(readbuf,"warn") != NULL)fprintf(fp_tmp,"%d\t%s",line,readbuf);}fclose(fp_src);//printf("%s_%s ok.\n",array[i],str);}else {fprintf(fp_tmp,"%s\n",array[i]);fprintf(fp_tmp,"warning!!! %s is down!!!!\n",array[i]);//printf("%s no.\n",array[i]);}seekdir(dp_ret2,0);}// move file pointer to head.fseek(fp_tmp,0,SEEK_SET);// fp_tmp append to fp_dst filewhile(fgets(readbuf,1024,fp_tmp)!=NULL)fprintf(fp_dst,"%s",readbuf);fclose(fp_dst);fclose(fp_tmp);// mkdir back_filecheck_dir(chk_dir);// move today file to backdir        for(i=0;i<COUNT;i++) {                while((ptr=readdir(dp_ret2))!=NULL) {                       // if(strstr(ptr->d_name,array[i]) && strstr(ptr->d_name,today_date) || strstr(ptr->d_name,array[i]) != NULL) {                        if(strstr(ptr->d_name,array[i]) && strstr(ptr->d_name,today_date) || strstr(ptr->d_name,array[i]) && strstr(ptr->d_name,today_date_4a) ) {//dup_file=ptr->d_name;                       // if(strstr(ptr->d_name,array[i]) && strstr(ptr->d_name,today_date)) {//sprintf(cmd2,"mv %s %s >/dev/null 2>&1",ptr->d_name,MYDIR);sprintf(cmd2,"mv %s %s/%s >/dev/null 2>&1",ptr->d_name,MYDIR,today_dir);//sprintf(cmd2,"mv %s %s/%s >/dev/null 2>&1",dup_file,MYDIR,today_dir);ret=system(cmd2);if(ret==-1) {fprintf(stderr,"system error.\n");exit(-1);}                                break;                        }                }seekdir(dp_ret2,0);}closedir(dp_ret2);}void last_month_file_handle() {char mark[32];char filename[32];char backdir[128];char january[32];static int month[12]={12,1,2,3,4,5,6,7,8,9,10,11};time_t t;struct tm *p1;t=time(NULL);p1=localtime(&t);sprintf(mark,"%02d",p1->tm_mday);if(!strcmp(mark,"12")) {// 当前月份sprintf(january,"%02d",1+p1->tm_mon);// 1月份时移动12月份的,只有这个特殊if(!strcmp(january,"01")) {// move file namesprintf(filename,"%d-%02d%s.txt",1900+p1->tm_year-1,month[p1->tm_mon],"*");printf("filename=%s\n",filename);// move dir namesprintf(backdir,"%d年巡检文件/%d月",1900+p1->tm_year-1,month[p1->tm_mon]);move_file(filename,backdir);} else {// move file namesprintf(filename,"%d-%02d%s.txt",1900+p1->tm_year,month[p1->tm_mon],"*");printf("filename=%s\n",filename);// move dir namesprintf(backdir,"%d年巡检文件/%d月",1900+p1->tm_year,month[p1->tm_mon]);move_file(filename,backdir);} }}int main(int argc,char *argv[]) {int sys_num;char *p,*str,path[128];static char *ugc_array[UGC_COUNT]={"BW-UGC-AMS01","BW-UGC-AMS02","BW-UGC-WBPS01","BW-UGC-WBPS02","BW-UGC-WPPS01","BW-UGC-WPPS02","BW-UGC-AS01","BW-UGC-AS02","BW-UGC-AS03","BW-UGC-AS04","BW-UGC-CS01","BW-UGC-CS02","BW-UGC-CS03","BW-UGC-CS04","BW-UGC-IFS01","BW-UGC-IFS02","BW-UGC-IFS03","BW-UGC-LRS01","BW-UGC-LRS02","BW-UGC-VES01","BW-UGC-VES02","BW-UGC-VES03","BW-UGC-TAS1","BW-UGC-BAKS01","BW-UGC-NMS01","testserver","db1","db2","zz-aq-4a-3","zz-aq-4a-4"};static char *enum_array[ENUM_COUNT]={"DBServer1","DBServer2","ENUMRPT"};static char *shipinmenhu_array[SHIPINMENHU_COUNT]={"BJBJ-BS-VWM-SV03-RPTDB01","BJBJ-BS-VWM-SV04-RPTDB02","BJBJ-BS-VWM-SV05-TRMDB01","BJBJ-BS-VWM-SV06-TRMDB02","BJBJ-BS-VWM-SV12-RPT01","BJBJ-BS-VWM-SV13-RPT02","BJBJ-BS-VWM-SV16-ESGM01","BJBJ-BS-VWM-SV17-ESGM02","BJBJ-BS-VWM-SV19-ESGM04","BJBJ-BS-VWM-SV24-ESGE01","BJBJ-BS-VWM-SV25-ESGE02","BJBJ-BS-VWM-SV26-ESGE03","BJBJ-BS-VWM-SV27-ESGE04","BJBJ-BS-VWM-SV28-ESGE05","BJBJ-BS-VWM-SV29-ESGE06","BJBJ-BS-VWM-SV30-PRTL01","BJBJ-BS-VWM-SV31-PRTL02","BJBJ-BS-VWM-SV32-PRTL03","BJBJ-BS-VWM-SV33-PRTL04","BJBJ-BS-VWM-SV34-PRTL05","BJBJ-BS-VWM-SV35-PRTL06","BJBJ-BS-VWM-SV36-PRTL07","BJBJ-BS-VWM-SV37-PRTL08","BJBJ-BS-VWM-SV38-PRTL09","BJBJ-BS-VWM-SV39-PRTL10","BJBJ-BS-VWM-SV40-PRTL11","BJBJ-BS-VWM-SV41-PRTL12","BJBJ-BS-VWM-SV42-PRTL13","BJBJ-PS-VWM-SV10-GFEP01","BJBJ-PS-VWM-SV11-GFEP02"};static char *SanGmenhu_array[SANGMENHU_COUNT]={"ZB-3GWAP-CLPS01","ZB-3GWAP-IPE01","ZB-3GWAP-PORTAL03","ZB-3GWAP-PORTAL04","ZB-3GWAP-PORTAL05","ZB-3GWAP-PORTAL08","ZB-3GWAP-PORTAL09","ZB-3GWAP-PORTAL13","ZB-3GWAP-PORTAL14","ZB-3GWAP-PORTAL15","ZB-3GWAP-PORTAL02","ZB-3GWAP-PORTAL06","ZB-3GWAP-PORTAL07","ZB-3GWAP-PROXY02","ZB-3GWAP-PROXY03","ZB-3GWAP-PROXY04","ZB-3GWAP-PROXY05","ZB-3GWAP-PROXY06","ZB-3GWAP-PROXY07","ZB-3GWAP-PROXY08","ZB-3GWAP-PROXY09","ZB-3GWAP-PROXY10","ZB-3GWAP-SCM01"};static char *neirongguanli_array[NEIRONGGUANLI_COUNT]={"ZB-CMS-ACK-01","ZB-CMS-ACK-02","ZB-CMS-ATK-01","ZB-CMS-ATK-02","ZB-CMS-DB01","ZB-CMS-DB02","ZB-CMS-DISP-01","ZB-CMS-DISP-02","ZB-CMS-DISP-03","ZB-CMS-EDIT-01","ZB-CMS-EDIT-02","ZB-CMS-EDIT-03","ZB-CMS-ICMS-01","ZB-CMS-UCE-01"};static char *shipinivr_array[SHIPINIVR_COUNT]={"IVVR-SV01-FILE01","IVVR-SV03-FILE02","IVVR-SV02-IVP01","IVVR-SV04-IVP02","IVVR-SV05-MSP01","IVVR-SV09-MSP02","IVVR-SV10-MSP03","IVVR-SV06-MGT"};static char *zonghewangguan_array[ZHWG_COUNT]={"ZB-SD-NMS-SVR-DB1-S","ZB-SD-NMS-SVR-DB2-S","ZB-SD-NMS-SVR-APP1-S","ZB-SD-NMS-SVR-APP2-S"};// argc argvif(argc>1) {fprintf(stderr,"Warn: input %s\n",argv[0]);exit(-1);}getcwd(path,sizeof(path));        for(p=strtok(path,"/");p;p=strtok(NULL,"/"))                str=p;// get dir        if(strstr(str,"shipinshangchuan")!=NULL)                 sys_num=1;else if(strstr(str,"ENUM-DNS")!=NULL)                sys_num=2;else if(strstr(str,"shipinmenhu")!=NULL)sys_num=3;else if(strstr(str,"3Gmenhu")!=NULL)sys_num=4;else if(strstr(str,"neirongguanli")!=NULL)sys_num=5;else if(strstr(str,"shipinivr")!=NULL)sys_num=6;else if(strstr(str,"zonghewangguan")!=NULL)sys_num=7;// file handleswitch(sys_num) {case 1: today_file_handle(ugc_array,UGC_COUNT);last_month_file_handle();break;case 2: today_file_handle(enum_array,ENUM_COUNT);last_month_file_handle();break;case 3: today_file_handle(shipinmenhu_array,SHIPINMENHU_COUNT);last_month_file_handle();break;case 4: today_file_handle(SanGmenhu_array,SANGMENHU_COUNT);last_month_file_handle();break;case 5: today_file_handle(neirongguanli_array,NEIRONGGUANLI_COUNT);last_month_file_handle();break;case 6: today_file_handle(shipinivr_array,SHIPINIVR_COUNT);last_month_file_handle();break;case 7: today_file_handle(zonghewangguan_array,ZHWG_COUNT);last_month_file_handle();break;default: printf("Don't find valid file.\n");}exit(0);}

 

原创粉丝点击