Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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:

Hi...
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;.....
Thanx...

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

Comment on Removing Windows newlines
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 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.
    HTH

    _________
    broquaint

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:
    :%s/^M//g
    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.

    TStanley
    --------
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:
    ctrlm.sh oldfile.txt >newfile.txt

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

    Teabag
    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

    Abigail

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.

    -P0w3rK!d

Re: Removing Windows newlines
by riffraff (Pilgrim) on May 14, 2003 at 18:34 UTC
    dos2unix filename.txt

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://258025]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-10-01 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls