Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Convert file to unix

by Anonymous Monk
on Aug 19, 2009 at 07:03 UTC ( #789707=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to convert file to dos to unix format. I tried using the command as below.
dos2Unix filename.txt > filename_new.txt
But filename_new.txt was a empty file.

Replies are listed 'Best First'.
Re: Convert file to unix
by james2vegas (Chaplain) on Aug 19, 2009 at 10:32 UTC
Re: Convert file to unix
by Anonymous Monk on Aug 19, 2009 at 07:12 UTC
    Maybe you should read the fine documentation for the fine dos2unix program? It comes with several examples.
Re: Convert file to unix
by JavaFan (Canon) on Aug 19, 2009 at 08:37 UTC
    Maybe because 'dos2Unix' is spelled 'dos2unix'? But what makes you think a Perl forum the appropriate place to ask about problems with an application?
Re: Convert file to unix
by Marshall (Abbot) on Aug 19, 2009 at 08:46 UTC
    A text file on Unix and a text file on Windows are basically the same. With ONE exception, for historical reasons a Windows text line ends in <CR><LF>, Carriage Return, Line Feed, while a Unix text line just ends in <LF>, Line Feed.

    Lots of FTP tools like FireFTP know about this and convert automatically. Update: Oh my gosh, I must admit that even Win IE know how to do this.

    I don't know how many files that you have to do this with, but say you have used binary ftp to transfer a "dos.text" to a Unix system, Open that file in vi and then save it. That blows way these <CR>'s, in Perl,'C', the \r chars away. That's it.

    However normally, if you FTP in "text mode" that happens automatically in either direction for you. Or at least that is very likely. This is NOT "rocket science": to go to Unix from Windows delete the \r characters. To go from Unix to Windows, add a \r before each \n that doesn't already have a \r before it.

      What version of nvi/vim is this? A co-admin on a server insints on ftping config files back and forth between the server and his windows box where he edits the files in notepad. So all the config files have the ^M disease and opening and saving in nvi or vim does not remove the ^Ms.
        In whatever flavour of vi you choose, type the following at the command (':') prompt i.e. non-visual:
        s,^M,,g w!
        NB The '^M' (<Ctrl> <M>) char is obtained by typing <Ctrl><V><Ctrl><M>...

        A user level that continues to overstate my experience :-))
Re: Convert file to unix
by Khen1950fx (Canon) on Aug 19, 2009 at 09:14 UTC
    Try using ExtUtils::Command.

    perl -MExtUtils::Command -e dos2unix filename.txt > filename_new.txt

      ExtUtils::Command::dos2unix doesn't print to stdout. It changes files in place.
Re: Convert file to unix
by doug (Pilgrim) on Aug 19, 2009 at 14:38 UTC

    I rolled my own once for a brain damaged environment that didn't have dos2unix. The "hard part" is s/\cM//g.

      You don't need the g modifier!!! ^M can appear only once at the end of each line :-)

        ^M can appear only once at the end of each line

        What sort of alternate universe are you living in? Have you never seen output from processes that do something like this?

        while( @to_do ) { # do something with next value of @to_do list... shift @todo; ... # report progress without causing the terminal to scroll: printf( " ... still %4d more to go\r", scalar @to_do ); }
        Of course, for stuff like that, just deleting all the "\r" characters will probably not produce results that you would really want. But my point is: do not assume there is ever any sort of guarantee that a DOS text file will always have exactly one CR for every LF, and/or that these will always appear as CRLF.

        (If you need another example, look at the manual for the unix "zip" command, esp. the "-l" option.)

Re: Convert file to unix
by whereiskurt (Friar) on Aug 19, 2009 at 19:48 UTC
    I literally type this every other day... I should write a script! :-)
    perl -pi -e 's/\r//' *.sh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://789707]
Approved by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-06-21 22:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.