removingCR/LF from unix and windows and mixed files

N

nntpman68

Hi,


I'm having files, which I'd like to slurp into an array
(one file per array)
However I'd like to get rid of the end of line characters of the files.
As files were created by windows users or linux users the lines will end
with either \n or with \r\n.
Additionally linux files might have been modified by windows users
(or vice versa) and not all editors are smart enough to adapt to the
files mode. so some files might have mixed line endings.

I came up with

@a = <$filehandle>
foreach $l (@a) { $l =~ s/(\r\n|\r)$//; }

or with

@a = grep { s/(\r\n|\r)$// } <$filehandle>

or with

@a=()
while(<$fh){ s/(\r\n|\r)$// ; push(@a); }

In above examples I could also replace the substitute with tr/\r\n//d


Is there already something like a strip_eof() function, or should I
stick with one of the above?



N
 
N

nntpman68

Opps, minor typo.

The last sentence should have been:
"Is there already something like a strip_eol() function"
and not "strip_eof()"
 
T

Tad J McClellan

nntpman68 said:
I'm having files, which I'd like to slurp into an array
(one file per array)
However I'd like to get rid of the end of line characters of the files.
As files were created by windows users or linux users the lines will end
with either \n or with \r\n.
Additionally linux files might have been modified by windows users
(or vice versa) and not all editors are smart enough to adapt to the
files mode. so some files might have mixed line endings.

I came up with

@a = <$filehandle>
foreach $l (@a) { $l =~ s/(\r\n|\r)$//; }


foreach $l (@a) { $l =~ s/\r?\n//; }
 
J

Jim Gibson

nntpman68 said:
Hi,


I'm having files, which I'd like to slurp into an array
(one file per array)
However I'd like to get rid of the end of line characters of the files.
As files were created by windows users or linux users the lines will end
with either \n or with \r\n.
Additionally linux files might have been modified by windows users
(or vice versa) and not all editors are smart enough to adapt to the
files mode. so some files might have mixed line endings.

I came up with

@a = <$filehandle>
foreach $l (@a) { $l =~ s/(\r\n|\r)$//; }

What about lines with just "\n" in them? This is a little shorter and
uses a character class instead of grouping and alternation:

s/[\r\n]+// for @a;
or with

@a = grep { s/(\r\n|\r)$// } <$filehandle>

You will drop lines that don't have "\r" in them. You should probably
use map instead of grep here.
or with

@a=()
while(<$fh){ s/(\r\n|\r)$// ; push(@a); }

In above examples I could also replace the substitute with tr/\r\n//d

That would be a good idea.
Is there already something like a strip_eof() function, or should I
stick with one of the above?

No. Perl's built-in functions are described in 'perldoc perlfunc' and
by 'perldoc -f xxx'. If you don't find something there, then you can
start looking at CPAN (<http://search.cpan.org>).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top