Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Removing Windows newlines

by Anonymous Monk
on May 14, 2003 at 10:51 UTC ( #258025=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The funny character "^M" appears somehow in a file I've got, and I cant figure out how to remove it... Example from file: 50514260;test_user;26.07.1999^M;.....

edited: Wed May 14 17:20:49 by jeffa - title change (was: silly problem)

Replies are listed 'Best First'.
Re: Removing Windows newlines
by broquaint (Abbot) on May 14, 2003 at 10:54 UTC
    This should do it
    perl -pi -e 's/\r\n?/\n/' yourfile
    See. perlrun for more info on perl's commandline options.


Re: Removing Windows newlines
by TStanley (Canon) on May 14, 2003 at 10:57 UTC
    The following will work, assuming that you are using some form of unix with the vi editor:
    To get the control-M character, you would first type control-V, followed by the control-M

    This problem also occurs if you are trying to ftp from a Windows system to unix, and you send the file in binary format. Change it to ascii, and that should fix your problem.

Re: Removing Windows newlines
by Joost (Canon) on May 14, 2003 at 10:54 UTC
    Very likely you're looking at the first part of the windows linebreak (\r\n). Try running this code with the filename as the argument:
    #!/usr/bin/perl -w use strict; while (my $fname = shift) { next unless -f $fname; open F,"<$fname" or die "Cannot open $fname: $!\n"; my $file = join('',<F>); close F; $file =~ s/\r\n?/\n/g; open F,">$fname" or die "Cannot write $fname: $!\n"; print F $file; close F; }
    See the Newlines section in perlport for more info.
    -- #!/usr/bin/perl -np BEGIN{@ARGV=$0}s(^([^=].*)|=)()s; =Just another perl hacker
Re: Removing Windows newlines
by teabag (Pilgrim) on May 14, 2003 at 11:06 UTC
    Always nasty, them windows newlines

    I always use a simple bash script to take care of this problem. It's not pretty, but it works for me.

    usage: oldfile.txt >newfile.txt

    #!/bin/bash cat $1 | tr -d "\015"

    Sure there's more than one way, but one just needs one anyway - Teabag

Re: Removing Windows newlines
by Abigail-II (Bishop) on May 14, 2003 at 12:06 UTC
    $ perl -0777pli -015e0 file file file


Re: Removing Windows newlines
by riffraff (Pilgrim) on May 14, 2003 at 18:34 UTC
    dos2unix filename.txt
Re: Removing Windows newlines
by P0w3rK!d (Pilgrim) on May 14, 2003 at 13:59 UTC
    At the command prompt:
    sed -e s/"^M"//g <infile> > <outfile>
    You can generate the ^M by pressing Ctrl+V then press M.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://258025]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-10-22 15:29 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (273 votes). Check out past polls.