Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > removingCR/LF from unix and windows and mixed files

Reply
Thread Tools

removingCR/LF from unix and windows and mixed files

 
 
nntpman68
Guest
Posts: n/a
 
      09-11-2008
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



 
Reply With Quote
 
 
 
 
nntpman68
Guest
Posts: n/a
 
      09-11-2008
Opps, minor typo.

The last sentence should have been:
"Is there already something like a strip_eol() function"
and not "strip_eof()"
nntpman68 wrote:
> 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
>
>
>

 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      09-11-2008
nntpman68 <(E-Mail Removed)> wrote:

>
> 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//; }


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      09-11-2008
In article <48c98b50$0$13865$(E-Mail Removed)>, nntpman68
<(E-Mail Removed)> wrote:

> 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>).

--
Jim Gibson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM
edif and vhdl files mixed Frank VHDL 6 10-23-2003 07:54 PM



Advertisments