remove CR + trailing spaces

Discussion in 'Perl Misc' started by Dan van Ginhoven, Dec 19, 2007.

  1. Hi!

    I had some files on my Linux server containing Dos/Win style CRLF and also
    trailing spaces on most lines.

    I ran this one-liner perl on them:
    cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname
    To my surprise both CR and LF was removed and I ended up with a file with
    just one long line.

    When I changed the one-liner to
    cat localfn | perl -p -e "s/\r//;s/\s*$/\n/;" > tempname
    it came out OK.

    What am I missing?
    Can any point me to the appropriate FAQ or man page please.


    /dg
     
    Dan van Ginhoven, Dec 19, 2007
    #1
    1. Advertising

  2. Dan van Ginhoven wrote:
    >
    > I had some files on my Linux server containing Dos/Win style CRLF and also
    > trailing spaces on most lines.
    >
    > I ran this one-liner perl on them:
    > cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname
    > To my surprise both CR and LF was removed and I ended up with a file with
    > just one long line.
    >
    > When I changed the one-liner to
    > cat localfn | perl -p -e "s/\r//;s/\s*$/\n/;" > tempname
    > it came out OK.
    >
    > What am I missing?


    The character class \s includes both the \r character and the \n
    character.

    perldoc perlre


    You could simplify that to:

    perl -lpe "s/\s+\z//" localfn > tempname


    Or if you want to modify localfn "in-place":

    perl -i.bak -lpe "s/\s+\z//" localfn



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Dec 19, 2007
    #2
    1. Advertising

  3. Dan van Ginhoven

    Ben Morrow Guest

    Quoth "Dan van Ginhoven" <>:
    >
    > I had some files on my Linux server containing Dos/Win style CRLF and also
    > trailing spaces on most lines.
    >
    > I ran this one-liner perl on them:
    > cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname


    Useless Use Of Cat.

    perl ... < localfn > tempname

    or use perl -i.

    > To my surprise both CR and LF was removed and I ended up with a file with
    > just one long line.


    \s matches \n, so this will remove all LFs.

    > When I changed the one-liner to
    > cat localfn | perl -p -e "s/\r//;s/\s*$/\n/;" > tempname
    > it came out OK.


    This will remove any blank lines, as well. You want

    s/[\r\t ]+$//;

    or (with 5.10)

    s/[\r\h]+$//;

    or use perl -l, which will remove the "\n" before doing the s///, and
    add it back on afterwards.

    Ben
     
    Ben Morrow, Dec 19, 2007
    #3
  4. Thanks John.
    >
    > The character class \s includes both the \r character and the \n
    > character.
    >
     
    Dan van Ginhoven, Dec 19, 2007
    #4
  5. Dan van Ginhoven

    Dr.Ruud Guest

    Dan van Ginhoven schreef:

    > cat localfn | perl -p -e "s/\r//;s/\s*$//;" > tempname
    > To my surprise both CR and LF was removed and I ended up with a file
    > with just one long line.


    Consider [[:blank:]] in stead of \s.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Dec 19, 2007
    #5
    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. Les Caudle
    Replies:
    0
    Views:
    672
    Les Caudle
    Mar 7, 2004
  2. pd
    Replies:
    3
    Views:
    482
    Jeff Higgins
    Dec 7, 2007
  3. John B. Matthews
    Replies:
    4
    Views:
    697
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    658
  5. johkar
    Replies:
    2
    Views:
    3,048
    Mayeul
    Dec 10, 2009
Loading...

Share This Page