in reply to Re: text matching
in thread text matching

Hi frozen,

This code dint work for me, It deleted all the code from the C program.

Regards,

-Prasad

Replies are listed 'Best First'.
Re^3: text matching
by frozenwithjoy (Priest) on Jun 16, 2012 at 06:30 UTC

    Hmmm. Can you show how you are running it? This is how my complete code looked:

    #!/usr/bin/env perl use strict; use warnings; open my $in, "<", shift; while( my $line = <$in> ) { $line =~ s/ .* report s? .* ; .* //sx; print $line; }

    Be sure to put the name of the report file as an argument on the CL. Also, which operating system are you using?

      I am running the code in cygwin and here is my complete code.

      #!/bin/perl5.8.6 use warnings; use diagnostics; use File::Basename; @files = <../tryremove/*>; #This is the destination path where the comment removed files are crea +ted. $path1 = "C:/Projects/tryremove/c"; foreach my $file (@files) { my $filename =basename($file); open(file1,"$file") or die "The file cannot be opened the $file:$!\n +"; open(file2,">$path1/$filename") or die "The file cannot be opened:$! +\n"; while(!eof(file1)){ $/ = undef; $_ = <file1>; s# .* report [s]? .* ; .* ##sx; print file2 ($_); } close(file1); close(file2); }

        Thanks. Please delete the line:

        $/ = undef;

        and let me know if it works.

        Here is the program using a different approach with the range operator.
        #!/usr/bin/perl use strict; use warnings; while (<DATA>) { if ((my $first = /^\s*#if/) .. (my $last = /^\s*#endif/)) { #print if $first || $last; print unless /^\s*reports?/; } else { print; } }

        Its a neat use of the range, ( .. ), operator.

        Chris

        Update: changed the first print line to more accurately perform what you needed.