在Windows中,对500k行文件执行dos2unix的最佳方法是什么? [英] What's the best way of doing dos2unix on a 500k line file, in Windows?

查看:130
本文介绍了在Windows中,对500k行文件执行dos2unix的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题全部说明了,我有一个500,000行文件是在Windows框上自动生成过程的一部分,并且被 ^ M 困扰.当它发布时,它需要 * nix 友好,这是最好的方法,是否有方便的代码片段可以帮助我实现这一目标?还是我需要编写一些C#或Java应用程序?

Question says it all, I've got a 500,000 line file that gets generated as part of an automated build process on a Windows box and it's riddled with ^M's. When it goes out the door it needs to *nix friendly, what's the best approach here, is there a handy snippet of code that could do this for me? Or do I need to write a little C# or Java app?

推荐答案

这是Perl的单行代码,取自 http://www.technocage.com/~caskey/dos2unix/

Here is a Perl one-liner, taken from http://www.technocage.com/~caskey/dos2unix/

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

您可以按以下方式运行它:

You can run it as follows:

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

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

Or, you can run it also in this way (the conversion is done in-place):

perl -pi dos2unix.pl file.dos

这是我的(原始)C版本:

And here is my (naive) C version:

#include <stdio.h>

int main(void)
{
   int c;
   while( (c = fgetc(stdin)) != EOF )
      if(c != '\r')
         fputc(c, stdout);
   return 0;
}

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

You should run it with input and output redirection:

dos2unix.exe < file.dos > file.unix

这篇关于在Windows中,对500k行文件执行dos2unix的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆