removingCR/LF from unix and windows and mixed files

Discussion in 'Perl Misc' started by nntpman68, Sep 11, 2008.

  1. nntpman68

    nntpman68 Guest

    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
     
    nntpman68, Sep 11, 2008
    #1
    1. Advertising

  2. nntpman68

    nntpman68 Guest

    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
    >
    >
    >
     
    nntpman68, Sep 11, 2008
    #2
    1. Advertising

  3. nntpman68 <> 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/"
     
    Tad J McClellan, Sep 12, 2008
    #3
  4. nntpman68

    Jim Gibson Guest

    In article <48c98b50$0$13865$>, 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)$//; }


    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
     
    Jim Gibson, Sep 12, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank

    edif and vhdl files mixed

    Frank, Oct 22, 2003, in forum: VHDL
    Replies:
    6
    Views:
    1,319
    Mike Treseler
    Oct 23, 2003
  2. Replies:
    12
    Views:
    1,653
    Dave Thompson
    Jan 10, 2005
  3. Replies:
    18
    Views:
    626
    Dave Thompson
    Jan 10, 2005
  4. Replies:
    3
    Views:
    1,309
    Walter Roberson
    May 1, 2006
  5. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    281
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page