Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

unix->dos & dos->unix

by Anonymous Monk
on Oct 10, 2002 at 03:45 UTC ( [id://204090]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
How do I convert unix files to dos files and dos files to unix files using perl at the unix shell prompt?

Replies are listed 'Best First'.
Re: unix->dos & dos->unix
by giulienk (Curate) on Oct 10, 2002 at 09:08 UTC
    To have true portability you should use hex codes:
    #DOS to UNIX perl -i.orig -pe 's/\015\012/\012/g' dosfile.txt
    Take a look at perlrun for the -i switch. And remember the following hash with newline for Windows, UNIX and Mac. I usually declare this on top of my program when i need to do lots of translation or outputting different kind of files
    %N = ( WIN => "\015\012", UNIX => "\012", MAC => "\015" );


    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

Re: unix->dos & dos->unix
by husoft (Monk) on Oct 10, 2002 at 03:47 UTC
    To convert a DOS text file to a Unix text file using Perl, enter:
    perl -p -e 's/\r$//' < dosfile.txt > unixfile.txt
    To convert from a Unix text file to a DOS text file with Perl, at the Unix shell prompt, enter:
    perl -p -e 's/$/\r/' < unixfile.txt > dosfile.txt
Re: unix->dos & dos->unix
by submersible_toaster (Chaplain) on Oct 10, 2002 at 06:20 UTC
    The SuperSearch yielded a relevant thread regarding this. To hurl in another few cents, Macintosh (I believe) uses a single "\r" or carriageReturn to terminate a line. There is also some quite strenuous discussion of the whole Unix Dos Mac Windows thing. Getting off topic now, since the question only regards doing this with shell oneliner.

    I searched the Q&A for a dummies guide to this, How to control UNIX vs DOS line feeds in Perl is pending approval by powers mightier than I

Re: unix->dos & dos->unix
by gnu@perl (Pilgrim) on Oct 10, 2002 at 13:10 UTC
    This is somewhat dependant on what os you are working under. In a unix environment there are native tools to do this.

    On SCO Unix the commands are 'dtox' (dos to unix) and 'xtod' (unix to dos).
    On Solaris the commands are 'dos2unix' and 'unix2dos'.
    In linux (Red Hat I know of, maybe others) 'fixdos'.

    I am sure that other *nix os'es have similar tools. Don't know if this helps with what you may be attempting to do, just thought I would add my 2.7 cents.

      The dos2unix and unix2dos tools have been ported to nearly every unix around and are available (on FreeBSD in the ports) even on win32.

      "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://204090]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-24 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found