代码之家  ›  专栏  ›  技术社区  ›  ninesided

在Windows中,在500k行文件上执行dos2unix的最佳方法是什么?[关闭]

  •  6
  • ninesided  · 技术社区  · 17 年前

    问题是,我有一个500000行的文件,作为Windows系统中自动构建过程的一部分生成,它充满了 ^ m 是的。当它出门时,它需要 *尼克斯 友好的,这里最好的方法是什么,有一个方便的代码片段可以为我做到这一点吗?或者我需要编写一个小的C或Java应用程序?

    7 回复  |  直到 17 年前
        1
  •  10
  •   Federico A. Ramponi    17 年前

    这是一个Perl-One衬板,取自 http://www.technocage.com/~caskey/dos2unix/

    #!/usr/bin/perl -pi
    s/\r\n/\n/;

    您可以按如下方式运行:

    perl dos2unix.pl < file.dos > file.unix
    

    或者,您也可以用这种方式运行它(转换是就地完成的):

    perl -pi dos2unix.pl file.dos
    

    这是我的(天真的)C版本:

    #include <stdio.h>
    
    int main(void)
    {
       int c;
       while( (c = fgetc(stdin)) != EOF )
          if(c != '\r')
             fputc(c, stdout);
       return 0;
    }
    

    您应该使用输入和输出重定向来运行它:

    dos2unix.exe < file.dos > file.unix
    
        2
  •  6
  •   Ken Gentle    17 年前

    如果安装底座 cygwin 太重,有很多独立的 dos2unix unix2dos 基于Windows的独立控制台程序在网上,很多有C/C++源码可用。如果我正确理解了需求,这些解决方案中的任何一个都可以很好地适应自动构建脚本。

        3
  •  5
  •   strager    17 年前

    如果您使用的是Windows,并且需要在批处理脚本中运行某个程序,那么您可以编译一个简单的C程序来完成这个技巧。

    #include <stdio.h>
    
    int main() {
        while(1) {
            int c = fgetc(stdin);
    
            if(c == EOF)
                break;
    
            if(c == '\r')
                continue;
    
            fputc(c, stdout);
        }
    
        return 0;
    }
    

    用途:

    myprogram.exe < input > output
    

    在适当的地方进行编辑会更加困难。此外,出于某种原因,您可能希望保留原始文件的备份(例如,在意外删除二进制文件的情况下)。

    那个版本删除了 全部的 CR字符;如果只想删除CR-LF对中的字符,可以使用(这是经典的单字符返回方法:-):

    /* XXX Contains a bug -- see comments XXX */
    
    #include <stdio.h>
    
    int main() {
        int lastc = EOF;
        int c;
        while ((c = fgetc(stdin)) != EOF) {
            if ((lastc != '\r') || (c != '\n')) {
                fputc (lastc, stdout);
            }
            lastc = c;
        }
        fputc (lastc, stdout);
        return 0;
    }
    

    您可以使用模式“R+”就地编辑文件。下面是一个通用的myd2u程序,它接受文件名作为参数。注意:这个程序使用ftruncate在末尾切掉多余的字符。如果有更好的(标准的)方法,请编辑或评论。谢谢!

    #include <stdio.h>
    
    int main(int argc, char **argv) {
        FILE *file;
    
        if(argc < 2) {
            fprintf(stderr, "Usage: myd2u <files>\n");
            return 1;
        }
    
        file = fopen(argv[1], "rb+");
    
        if(!file) {
            perror("");
            return 2;
        }
    
        long readPos = 0, writePos = 0;
        int lastC = EOF;
    
        while(1) {
            fseek(file, readPos, SEEK_SET);
            int c = fgetc(file);
            readPos = ftell(file);  /* For good measure. */
    
            if(c == EOF)
                break;
    
            if(c == '\n' && lastC == '\r') {
                /* Move back so we override the \r with the \n. */
                --writePos;
            }
    
            fseek(file, writePos, SEEK_SET);
            fputc(c, file);
            writePos = ftell(file);
    
            lastC = c;
        }
    
        ftruncate(fileno(file), writePos); /* Not in C89/C99/ANSI! */
    
        fclose(file);
    
        /* 'cus I'm too lazy to make a loop. */
        if(argc > 2)
            main(argc - 1, argv - 1);
    
        return 0;
    }
    
        4
  •  4
  •   hayalci    17 年前
    tr -d '^M' < infile > outfile
    

    您将键入^M作为:ctrl+v,输入

    编辑 :您可以使用'\r'而不是手动输入回车,,[ 感谢@strager ]

    tr -d '\r' < infile > outfile
    

    编辑2 :'tr'是一个Unix实用程序,您可以从下载本机Windows版本 http://unxutils.sourceforge.net [ 感谢@Rob Kennedy 或使用 cygwin 的Unix仿真。

        5
  •  1
  •   EvilTeach    17 年前

    以ASCII文件的形式,而不是二进制文件的形式,将其从DOS框传输到Unix框。 ftp将删除 慢性淋巴细胞白血病 并插入一个 低频 . 将其作为二进制文件传输回DOS框,然后 低频 将被保留。

        6
  •  1
  •   nickf    17 年前

    一些文本编辑器,例如 UltraEdit/UEStudio 内置此功能。

    File > Conversions > DOS to UNIX

        7
  •  -2
  •   Paul    17 年前

    如果只是一个文件,我使用记事本++。很好,因为它是免费的。我已经安装了cygwin,并使用我为多个文件编写的一行脚本。如果您对脚本感兴趣,请留言。(我现在还没拿到。)