过滤多个文件中带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);}
- 过滤多个文件中带warn的行,将该行保留到新文件中
- 将文件夹下的多个文件的内容合并到一个文件中
- 将一个文件夹下的多个文件合并到一个文件中
- 关于ultraedit的使用,利用正则表达式过滤有用信息到新的文件中
- 将Dataset中的多个DataTable导入到一个Excel文件的多个Sheet中
- java io实例一:将2个文件中的内容写入一个新的文件中
- Java文件中某一行出现错误时,鼠标停留到该行MyEclipse不提示错误信息
- c#(Asp.net)多个Excel文件合并到一个新的excel中
- 查找文件中包含某个字符串的行并将所有满足条件行输出到新文件使用命令
- 将文件中内容按行写入多个文件
- ASP.NET将数据导出到一个EXCEL文件的多个SHEET中
- Python:将一个文件中的每一行复制到一个新创建的文件中,并计算文件中总共有多少行
- 利用asp将数据写入到新的excel文件中
- 利用asp将数据写入到新的excel文件中
- 将'文件1'的内容复制到'文件2'中
- 从一组文件名中保留某些同名且带后缀的文件
- 关于adb push 带空格的文件到机台中
- 复制多个文件到一个文件中
- 获得星期几的程序--基姆拉尔森计算公式
- 未加索引的外键(unindexed foreign keys)
- float数据在内存中的存储方法
- DNS污染与DNS劫持
- Java设计模式
- 过滤多个文件中带warn的行,将该行保留到新文件中
- oracle数据类型
- 国外编程网站
- loadView VS viewDidLoad
- oracle 修改用户密码
- Net学习网站大全
- Ubuntu中使用ROOT帐号
- asp 网站
- 面向对象设计原则