再次求助。bat代码

bnmjk36 2014-09-05 09:56:43
有2个记事本:记事本a,记事本b

记事本a 的内容

123
321
5643
6843
7676
123
321
5643
6843
7676



记事本b 的内容(包含记事本a的内容)

123
321
5643
6843
7676
123
321
5643
6843
7676
7654
5645
46674
64346754
64346754



我想得到记事本b 的结果 记事本b中 只要是记事本a 的数据都要删掉 一个不留。且记事本b 原行的重复数据 不在重复 各个保留一个

7654
5645
46674
64346754
...全文
256 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿呆_ 2014-09-07
  • 打赏
  • 举报
回复
引用 10 楼 bnmjk36 的回复:
findstr /v /x /g:a.txt b.txt > c.txt 这个能把只要是a。txt的内容从b。txt都过滤,却不能同时把b。txt重复过滤成不重复的
所以bat中这个findstr的结果用FOR循环再进行了第二次过滤呀(过滤掉重复行)
bnmjk36 2014-09-06
  • 打赏
  • 举报
回复
findstr /v /x /g:a.txt b.txt > c.txt 这个能把只要是a。txt的内容从b。txt都过滤,却不能同时把b。txt重复过滤成不重复的
阿呆_ 2014-09-05
  • 打赏
  • 举报
回复
随便写了一个,没有优化,使用时类似调用xxx.bat A.txt B.txt,第一个参数是文本A,第二个参数是文本B,结果输出到屏幕(如果你希望输出到文件那么用 xxx.bat A.txt B.txt > C.txt输出到C.txt) 有使用限制:不能识别大小写,不能包含空行, 行中不能包含'=':
@echo off
setlocal

for /f usebackq^ delims^=^ eol^= %%A in (`findstr /vxg:%1 %2`) do (
  if not defined _%%A (
    echo %%A
    set "_%%A=1"
  )
)
mujiok2003 2014-09-05
  • 打赏
  • 举报
回复
记事本? 文本文件吧?
bnmjk36 2014-09-05
  • 打赏
  • 举报
回复
我想得到结果是 记事本b中 只要是记事本a 的数据都要删掉 一个不留。且记事本b 原行的重复数据 不再重复(但每个保留一个) 7654 5645 46674 64346754
bnmjk36 2014-09-05
  • 打赏
  • 举报
回复
Idle_l老师 您这个代码不能实现我想要的结果
勤奋的小游侠 2014-09-05
  • 打赏
  • 举报
回复
在cmd窗口输入bat文件名a,b文件名作参数应该会吧?
阿呆_ 2014-09-05
  • 打赏
  • 举报
回复
引用 5 楼 bnmjk36 的回复:
谢谢大家的回答,我这些都不懂,我只要bat的完整代码,就如Idle_ 老师的这种形式,但是我不知道把记事本a.txt,b.txt放在代码的哪里,期待大家再次给回复 就像版主那样的答案。 大家不要笑我,我从来没学过这些东西。
我给你的bat代码中a.txt, b.txt是作为调用bat时传的参数,如果你要写死,那么代码中%1就是a.txt, %2就是b.txt,你自己替换就是了。
bnmjk36 2014-09-05
  • 打赏
  • 举报
回复
谢谢大家的回答,我这些都不懂,我只要bat的完整代码,就如Idle_ 老师的这种形式,但是我不知道把记事本a.txt,b.txt放在代码的哪里,期待大家再次给回复 就像版主那样的答案。 大家不要笑我,我从来没学过这些东西。
赵4老师 2014-09-05
  • 打赏
  • 举报
回复
仅供参考
//输出PROG中有但LIST中没有的文本行,即集合PROG-LIST
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#define MAXCHARS 512
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
char PROG[256]="PROG";//程序Program需要的文件列表
char LIST[256]="LIST";//dir /b /s生成的实际文件列表List
FILE *fp,*fl;
int i,c,n,L,hh;
int ignore_case=0;
char ln[MAXCHARS];
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc>1) strcpy(PROG,argv[1]);//命令行参数1覆盖PROG
    if (argc>2) strcpy(LIST,argv[2]);//命令行参数2覆盖LIST
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    if ((fl=fopen(LIST,"rt"))==NULL) {
        fprintf(stderr,"Can not open %s\n",LIST);
        fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
        return 1;
    }
    if ((fp=fopen(PROG,"rt"))==NULL) {
        fclose(fl);
        fprintf(stderr,"Can not open %s\n",PROG);
        fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
        return 2;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(fl);
        fclose(fp);
        fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 4;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (fgets(ln,MAXCHARS,fl)==NULL) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",LIST,hh,MAXCHARS);
            while (1) {
                c=fgetc(fl);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    free(buf);
                    fclose(fl);
                    fclose(fp);
                    fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    return 5;
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(fl);
    if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
    else qsort(buf,n,MAXCHARS,compare);
    hh=0;
    while (1) {
        if (fgets(ln,MAXCHARS,fp)==NULL) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",PROG,hh,MAXCHARS);
            while (1) {
                c=fgetc(fp);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            if (ignore_case) {
                if (NULL==bsearch(ln,buf,n,MAXCHARS,icompare)) printf("%s\n",ln);
            } else {
                if (NULL==bsearch(ln,buf,n,MAXCHARS,compare)) printf("%s\n",ln);
            }
        }
    }
    fclose(fp);
    free(buf);
    return 0;
}
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128      //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
   return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
   return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
    if (argc<3) {
        printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
        printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
        return 1;
    }
    if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
    f=fopen(argv[1],"r");
    if (NULL==f) {
        printf("Can not find file %s!\n",argv[1]);
        return 1;
    }
    buf=(char *)malloc(MAXLINES*MAXCHARS);
    if (NULL==buf) {
        fclose(f);
        printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
        return 2;
    }
    n=0;
    hh=0;
    i=0;
    while (1) {
        if (NULL==fgets(ln,MAXCHARS,f)) break;//
        hh++;
        L=strlen(ln)-1;
        if ('\n'!=ln[L]) {//超长行忽略后面内容
            printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
            while (1) {
                c=fgetc(f);
                if ('\n'==c || EOF==c) break;//
            }
        }
        while (1) {//去掉行尾的'\n'和空格
            if ('\n'==ln[L] || ' '==ln[L]) {
                ln[L]=0;
                L--;
                if (L<0) break;//
            } else break;//
        }
        if (L>=0) {
            strcpy(buf+i,ln);i+=MAXCHARS;
            n++;
            if (n>=MAXLINES) {
                MAXLINES2=MAXLINES*2;
                if (MAXLINES2==1280000) MAXLINES2=2500000;
                buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
                if (NULL==buf2) {
                    printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
                    printf("WARNING: Lines >%d ignored.\n",MAXLINES);
                    break;//
                }
                buf=buf2;
                MAXLINES=MAXLINES2;
            }
        }
    }
    fclose(f);
    if (n>1) {
        if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
        else qsort(buf,n,MAXCHARS,compare);
    }
    f=fopen(argv[2],"w");
    if (NULL==f) {
        free(buf);
        printf("Can not create file %s!\n",argv[2]);
        return 2;
    }
    fprintf(f,"%s\n",buf);
    if (n>1) {
        if (ignore_case) {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        } else {
            hh=0;
            L=MAXCHARS;
            for (i=1;i<n;i++) {
                if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
                    fprintf(f,"%s\n",buf+L);
                }
                hh=L;
                L+=MAXCHARS;
            }
        }
    }
    fclose(f);
    free(buf);
    return 0;
}
jiuchang 2014-09-05
  • 打赏
  • 举报
回复
这个功能不需要写代码,用linux命令行即可完成
sort -u a >> a.u
sort -u b >> b.u
cat a.u 123 321 5643 6843 7676
cat b.u
123 321 46674 5643 5645 64346754 6843 7654 7676
comm -3 a.u b.u
46674 5645 64346754 7654 另外可以在执行sort命令中使用-n参数,这样排序时是按数字大小排序的

3,882

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧