Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Print contents of text file line by line

by Marshall (Canon)
on Jun 23, 2018 at 22:03 UTC ( #1217307=note: print w/replies, xml ) Need Help??


in reply to Print contents of text file line by line

#!/usr/bin/perl use strict; use warnings; my $filename='result.txt'; open( my $fh, '<', $filename ) or die "Can't open $filename: $!"; while ( my $line = <$fh> ) { $line =~ s/\s*$//; # remove any line ending char(s) # this works on Windows, Unix and old Apple print "$line\n"; # print with line endings for this platform } close $fh;
The substitution expression is to get rid of any combination of
<line feed>,<carriage return>.<space> at end end of the line.

Unix uses <LF> to separate lines.
Windows uses <CR> then <LF> to separate lines. Old Apple uses <CR>.

The print uses the line endings for the platform that you are using.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2023-03-20 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (59 votes). Check out past polls.

    Notices?