Professional Web Applications Themes

HOT: Search/Replace || and *| - PERL Beginners

I have a file that I need to replace the *| and || with just | and can not seem to get it to go. It either puts in an | every space, or it puts in multiples of them, how do I fix this? here's what I have: sub cleanup{ use strict; my $file = "/home/web/sales/info/test/custs1"; my $newfile = "/home/web/sales/info/test/fixed.txt"; my $line; open (OLDFILE, "< $file"); open (NEWFILE, "> $newfile"); while ($line = <OLDFILE>) { $line =~ s/||/|/mg; $line =~ s/\*|/|/mg; print NEWFILE "$line\n"; } close OLDFILE; close NEWFILE; print "$newfile has now been created\n"; }...

  1. #1

    Default HOT: Search/Replace || and *|

    I have a file that I need to replace the *| and || with just | and can not
    seem to get it to go. It either puts in an | every space, or it puts in
    multiples of them, how do I fix this?

    here's what I have:
    sub cleanup{

    use strict;

    my $file = "/home/web/sales/info/test/custs1";
    my $newfile = "/home/web/sales/info/test/fixed.txt";
    my $line;

    open (OLDFILE, "< $file");
    open (NEWFILE, "> $newfile");
    while ($line = <OLDFILE>) {
    $line =~ s/||/|/mg;
    $line =~ s/\*|/|/mg;
    print NEWFILE "$line\n";
    }
    close OLDFILE;
    close NEWFILE;

    print "$newfile has now been created\n";
    }

    Lonewolf Guest

  2. #2

    Default Re: HOT: Search/Replace || and *|

    On Fri, 2 Apr 2004 15:19:13 -0800 (PST)
    LoneWolf <rr.com> wrote:
     

    The '|' character is a special character in a Perl RE. Try escaping it.
    Also why are you using the 'm' flag in the substitute operator? I do
    not think you need it.
     


    --
    Smoot Carl-Mitchell
    Systems/Network Architect
    email: com
    cell: +1 602 421 9005
    home: +1 480 922 7313
    Smoot Guest

  3. #3

    Default Re: HOT: Search/Replace || and *|

    Lonewolf wrote: 

    You should *ALWAYS* verify that the files were actually opened.
     

    You should declare $line here to limit its scope.
     

    The '|' character is special in a regular expression. /||/ means "match
    '' or '' or ''". You have to escape special characters to match the
    literal character. The /m option is used with the line anchors ^ and $
    but since you are not using them it is not required.
     


    use warnings;
    use strict;

    my $dir = '/home/web/sales/info/test';
    my $file = "$dir/custs1";
    my $newfile = "$dir/fixed.txt";

    open OLDFILE, "< $file"; or die "Cannot open $file: $!"
    open NEWFILE, "> $newfile" or die "Cannot open $newfile: $!";

    while ( my $line = <OLDFILE> ) {
    # use a character class to specify either '*' or '|'
    # character classes don't require '*' or '|' to be escaped
    $line =~ s/[*|]\|/|/g;
    print NEWFILE "$line\n";
    }
    close OLDFILE;
    close NEWFILE;

    print "$newfile has now been created\n";




    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Search and replace
    By John McDermidt in forum Adobe Acrobat SDK
    Replies: 6
    Last Post: December 17th, 10:28 AM
  2. Search & Replace in site
    By Karolus in forum Dreamweaver AppDev
    Replies: 0
    Last Post: May 15th, 03:36 PM
  3. Search and replace (super global replace)
    By johnweiffenbach@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 1
    Last Post: April 8th, 08:56 AM
  4. search an replace
    By Rmck in forum PERL Beginners
    Replies: 4
    Last Post: January 22nd, 04:52 PM
  5. search replace
    By Pandey Rajeev-A19514 in forum PERL Beginners
    Replies: 1
    Last Post: September 6th, 04:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139