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

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

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

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,616
    Mike Treseler
    Oct 23, 2003
  2. Jay O'Connor

    IDLE on Windows; save files as UNIX?

    Jay O'Connor, Feb 29, 2004, in forum: Python
    Replies:
    3
    Views:
    1,158
    Paxcal
    Mar 1, 2004
  3. Travis James Kleeburg

    Files in unix and windows

    Travis James Kleeburg, Aug 20, 2004, in forum: Python
    Replies:
    3
    Views:
    559
    Byron
    Aug 21, 2004
  4. **--> That Guy Downstairs
    Replies:
    14
    Views:
    964
    S7Solutions
    Feb 21, 2006
  5. Replies:
    3
    Views:
    1,534
    Walter Roberson
    May 1, 2006
  6. higer
    Replies:
    7
    Views:
    666
    Piet van Oostrum
    Jun 13, 2009
  7. Replies:
    1
    Views:
    194
    Jerry C
    Jul 13, 2006
  8. Huppu
    Replies:
    3
    Views:
    486
    Huppu
    Nov 27, 2003
Loading...