Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

crlf to \n

by rgb96 (Acolyte)
on Apr 02, 2009 at 14:33 UTC ( [id://754982]=perlquestion: print w/replies, xml ) Need Help??

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

Hey Monks,

I'm taking a file that was written in notepad, importing it to my linux machine, and trying to chomp the lines and display them in a tree view. As I quickly found out, chomp won't work with it's default value because windows format uses crlf and not \n. Quick question, is it best to just change the value of $/ ? And if so, is this the way i would do it?

local $/ = "\r\n"; open(FILE, "environments.txt); my @temp = <FILE>; foreach my $line (@temp){ chomp($line); print $line; }

print used instead of treeview just for illustration

Replies are listed 'Best First'.
Re: crlf to \n
by moritz (Cardinal) on Apr 02, 2009 at 14:36 UTC
    open my $file, '<:crlf', 'environments.txt' or die $!; while (@temp) { chomp; print; }

    The crlf IO layer converts crlf into \n while reading.

      Thanks guys, those will work.
Re: crlf to \n
by planetscape (Chancellor) on Apr 02, 2009 at 15:21 UTC
Re: crlf to \n
by kennethk (Abbot) on Apr 02, 2009 at 14:41 UTC
    I transfer files between OS's on fairly regular basis. My solution is to just run the following one-liner as part of the transfer procedure - that way it doesn't matter what I feed it into on the far end.

    perl -pi -e 's/\r\n/\n/;' filename

    Information on the command-line switches above is found in perlrun.

      or just:
      perl -pi -e 'tr[\r][]d' environments.txt

      print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
Re: crlf to \n
by dwm042 (Priest) on Apr 02, 2009 at 18:26 UTC
    Since it hasn't been mentioned, and for completeness (planetscape's methods would be my recommendation for automated solutions):

    1. load file into vi 2. in command mode: %s/^V^M//g (^V is literal mode, then add carriage return, or ^M) 3. save file.
    This might work with vim if you use vi compatibility mode; vim -C iirc. Otherwise vim tends to ignore eol differences.

    Update: Windows is not absolutely bound to crlf eols either. Load a Unix file into wordpad sometime. That's the classic unix eol test in Win32: looks funny in notepad, looks fine in wordpad.

      In vim you can use 'set ff=dos' or 'set ff=unix' to save file with desired line-endings. So you can load file into vim, type :set ff=unix, and save file.

        It's possibly worth noting that merely changing the filetype fileformat (ff) in Vim won't repair a file which has a mix of *nix and dos line-endings, and because of the still massive userbase of a few specific Microsoft tools (that are brain-dead on this score), such files are rather commonly encountered. In such a case one has to actively do something, and if one wants to do something inside Vim, it ought to work to do what dwm042 wrote in reply above.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found