#
# Programname: fpg - Frams' Perl grep
# Author: framstag@rus.uni-stuttgart.de
-# Copyright: GPL
+# Licence: Perl Artistic
#
# History:
# 2003-02-27 Framstag initial version
# -n ==> -S, new -n option
# 2008-10-14 Framstag added option -M
# 2008-11-23 Framstag added option -~
+# 2016-06-12 Framstag option -o respects (match)
use Getopt::Std;
use Term::ReadLine;
use locale;
-sub usage {
- die <<EOD
+$0 =~ s:.*/::;
+$| = 1;
+
+$usage = <<EOD;
usage: $0 [options] 'EXP' [file...]
or: $0 [options] -Q file...
options: -r recursively scan through directories
-l list filenames only
-L list filenames only that do NOT match
-p show paragraphs, not lines (multiline record separator)
- -o show only matched strings, not whole lines
+ -o show only matched strings (in parenthesis), not whole lines
-M mail-mode: search and show complete mails from mbox files
-c print (count) only number of matches (NOT LINES!)
-F EXP is a string, not a Perl regular expression
#examples: $0 -r 'from.*STDIN' *
# $0 -e 'length>30 and not /\\w/' script
#See "perldoc perlre" for help on regular expressions.
-}
-$0 =~ s:.*/::;
-$| = 1;
$maxlen = 0;
$opt_x = $opt_X = '';
$opt_R = "\n";
-usage() if !getopts('hirvlLFMopscQen~S:R:C:x:X:') or $opt_h and not @ARGV;
+getopts('hirvlLFMopscQen~S:R:C:x:X:') or die $usage;
+
+if ($opt_h) {
+ print $usage;
+ exit;
+}
unless ($opt_Q) {
- $exp = shift or usage();
+ $exp = shift or die $usage;
}
if ($opt_C and ($opt_l or $opt_L or $opt_s or $opt_v or $opt_p or $opt_M)) {
} else {
$exp =~ s/([\@\$\%\^\&\*\(\)\+\[\]\{\}\\\|\.\?])/\\$1/g if $opt_F;
$exp = '(?i)'.$exp if $opt_i;
- $exp = '(?s)'.$exp if $opt_p or $opt_R;
+ $exp = '(?s)'.$exp if $opt_p or $opt_R ne "\n";
#? $exp =~ s/\.\*\*/[.\n]*/g;
}
else { $n++ while /$exp/omg }
} else {
if ($opt_o) {
- my $m = '';
- while (s/($exp)//) {
- $n++;
- $m .= "$1\n";
+ if ($exp =~ /\([^?]+\)/) {
+ if (/$exp/) {
+ $n++;
+ $_ = "$1\n";
+ }
+ } else {
+ my $m = '';
+ while (s/($exp)//) {
+ $n++;
+ $m .= "$1\n";
+ }
+ $_ = $m;
}
- $_ = $m;
} elsif ($opt_Q) {
$n += s/($exp)/$B$1$N/mg;
} else {