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

newlines for windows from unix

by Anonymous Monk
on Jun 07, 2001 at 17:02 UTC ( [id://86532]=perlquestion: print w/replies, xml ) Need Help??

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

Hello. I've tried chr(015), 013, 10, 13, everything btwn. 010, 015 and 10 - 15. I always get blocks when opening the file in windows notepad. How can I reliably print windows newlines from perl scripts running on Linux & writing to a local file?

Replies are listed 'Best First'.
Re: newlines for windows from unix
by jeroenes (Priest) on Jun 07, 2001 at 17:12 UTC
    Run your files through:
    perl -i.bak -pe 'chomp; $_.="\r\n";'
    Jeroen
    "We are not alone"(FZ)

      My favorite switch is -l, and this is why:
      perl -i.bak -ple 'BEGIN{$/="\n"}'
      gets you from Unix to DOS (or should) and changing "\n" to "\r\n" gets you back.

      Update: assuming you're always converting to the system you run the command on, that is--the -l flag gives you native line-endings.

      Though of course if you're in MSDOS, you presumably need to fiddle with the quotes some.



      If God had meant us to fly, he would *never* have give us the railroads.
          --Michael Flanders

Re: newlines for windows from unix
by mr.nick (Chaplain) on Jun 07, 2001 at 17:12 UTC
    The combination (from Linux) of "\r\n" will produce the results you are looking for when you open a file in Windows.
    print "Hello\r\nWorld\r\n";

    However, if you are running Perl from Windows, then a simple "\n" will suffice.

(tye)Re: newlines for windows from unix
by tye (Sage) on Jun 07, 2001 at 19:38 UTC
    perl -i.can -please file is a general-purpose newline fixer. When you run it on Windows, it will turn Unix-style newlines into Windows-style newlines (and will leave Windows-style newlines alone). When you run it on Unix, it will turn Windows-style newlines into Unix-style newlines (and will leave Unix-style newlines alone).

    Unfortunately, converting to and from Mac-style newlines isn't covered by this.

    Don't forget to eventually throw the backups into the trash can:

    rm *.can del *.can

    If you want to use wildcards with this command unders Windows, then put the following into a file called "atch.pm" under perl/site/lib: @ARGV= map { glob } @ARGV; then use a command like this: perl -Match -i.can -please *.ext

    This message was sponsored by The World League For Cute But Useful Idioms.

    Update: {grumble} ChemBoy just made me realize that this only works when run under Win32. Oh well, perl -i.can -pley/\\r//d should work even for Win32 files on a Mac and on Mac files under Unix and Win32, except that you should not double the \ under Win32 and I don't know whether you should on a Mac. Note that this solution also removes "\r"s from the middle of lines. We need to remove them from front of lines for converting between Win32 and Mac formats and from the ends of lines for Win32-to-Unix conversion.

            - tye (but my friends call me "Tye")
Re: newlines for windows from unix
by davorg (Chancellor) on Jun 07, 2001 at 17:15 UTC
Re: newlines for windows from unix
by DrSax (Sexton) on Jun 07, 2001 at 17:30 UTC
    Here is the code for a filter that should do the trick.
    #! /usr/local/bin/perl -w use IO::File; while ($string = <STDIN>) { chomp($string); STDOUT->print("$string\r\n"); }
    Of course, this begs the question of why, once you have a file that is not contaminated by newlines you would want to add them for the sake of Notepad, but I will leave that discussion to others... :-)

    Brian - a.k.a. DrSax
Re: newlines for windows from unix
by Tiefling (Monk) on Jun 07, 2001 at 17:52 UTC
    On a related note - how can I strip out Windows newlines from the WordPad-compiled files I now1 intend using in Linux?

    Thanks!

    Tiefling

    1: In other words, they already exist. If I were writing them now, I'd write them in a Linux text editor.
      I have a script, chmod +x, in ~/bin:
      #!/usr/bin/perl -i -p tr/\015//d;
      and I run it like: removecarr.pl ugly_whoze_file Jeroen
      "We are not alone"(FZ)
      May I suggest that you use a proper text editor in windows. That way your text files are not stored with the windows new line rubbish on the end.

      I use conText which I downloaded for free from the internet and which I can highly recommend (nothing can be too bad if it is free, but this is truely good ;)).

      As to your question. I have a script loops through each line of files doing a sub for 012 and 015, replacing them with nothing. In that way you remove any newlines present. Then add an 'if' keying on $^0 eg if($^0 eq 'wnt'){..... to check the OS and add the suitable newline.

      This is clunky but simple to write and quaranteed to work.

      Hope that this helps.

      "The significant problems we face in life can not be solved at the same level of thinking we were at when we created them." -Albert Einstein

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found