#!/usr/bin/perl -w # # Loops through the input (input taken from files on the command line or from STDIN) # line by line (or file by file) and prints what was captured in the first # parenthesis of the pattern. Start the pattern with (?i) for case insensitive matching. # Use -m to match across multiple lines. # # Usage: mygrep [-m] [files...] # # @author Peter Marklund (peter@collaboraid.biz) if ($ARGV[0] =~ /^-m/) { shift; # Undefine the record separator to read the whole file in one go undef $/; $pattern = shift; # Let . match newline if ($pattern !~ /^\w*\(\?\w*s\w*\)/) { $pattern = "(?s)$pattern"; } } else { $pattern = shift; } # Take input from STDIN or from files my $have_match = 0; while ($line = <>) { while ($line =~ /$pattern/) { if (defined($1)) { print STDOUT "$1\n"; } # Do next matching on whatever comes after last match $line=$'; $have_match = 1; } } $have_match ? exit 0 : exit 1;