mikevanhoff has asked for the wisdom of the Perl Monks concerning the following question:

I have a text file in ansi 837 format. There are no carriage returns in the file. Each occurrance of a tilde "~" needs to be replaced with a Carrige return. Can someone offer my some advice on how to accomplish this? Thanks in advance.

Replies are listed 'Best First'.
Re: Search and Replace
by broquaint (Abbot) on Jun 09, 2003 at 16:55 UTC
    Combine the -p and -i switch to get a handy one-liner
    perl -i.bak -pe 'tr[~][\r]' yourfile
    See. perlrun for more info on the switches used above.
    HTH

    _________
    broquaint

Re: Search and Replace
by arthas (Hermit) on Jun 09, 2003 at 16:55 UTC

    Should you need to the non-oneliner solution, you might want to try the following:

    open (INFILE, "<myfile.txt"); open (OUTFILE, "<myfile.txt"); while (my $row = <INFILE>) { $row =~ s/~/\r/g; print OUTFILE $row; } close (INFILE); close (OUTFILE);

    Michele.

      Thank you for help. That did the trick. However, I seem to have found a problem. Since the results file will be used to load a database the "^M" cannot appear in the file. instead of getting: "this that the-other-thing^MHere is the other thing^M" I need to get: "this that the-other-thing" "Here is the other thing" Can you help with this? Thanks in advance
        These two solutions are replacing tildes with carriage return characters (\r or \x0D). If you want line endings, then you should replace tildes with \n like someone else showed. That will turn into the line ending characters for your platform (CRLF, LF).
Re: Search and Replace
by gellyfish (Monsignor) on Jun 09, 2003 at 16:57 UTC
    Assuming your file is called "tt.txt" then this should do the job when run from the command line:
    perl -pi -e's/~/\n/g' tt.txt
    You might want to look at the perlrun and perlre manpages for more information on what is going on there.
    /J\
    
Re: Search and Replace
by svsingh (Priest) on Jun 09, 2003 at 20:44 UTC