in reply to Regex in a printfile?

use List::MoreUtils qw( apply ); print OUTFILE apply { s/\s*#.*//; } grep { !/^\s*#/ } <INFILE>;

If you want to keep the lines that contained nothing but comments, remove the grep.

List::MoreUtils is not a core module, but it is a very useful module. It can be installed using ppm install List-MoreUtils if you're using ActivePerl.

Replies are listed 'Best First'.
Re^2: Regex in a printfile?
by Andrew_Levenson (Hermit) on Nov 16, 2006 at 02:00 UTC
    Is there any way to do it without using any modules? (Just curious.)

    Thanks!

      huh, there's already a solution that doesn't use any modules in this thread.

      But apply can be implemented using map pretty easily.

      print OUTFILE map { my $s = $_; $s =~ s/\s*#.*/; $s } grep { !/^\s*#/ } <INFILE>;

      You could even merge the map with the grep, but then it looks like spagetti.

        Hey, I understand that! Well, all except the final $s in map. What does that do?
        Thanks.