#! /bin/sh
# showmatch - mark string that matches pattern
pattern=$1; shift
awk 'match($0,pattern) > 0 {
s = substr($0,1,RSTART-1)
m = substr($0,1,RLENGTH)
gsub (/[^\b- ]/, " ", s)
gsub (/./, "^", m)
printf "%s\n%s%s\n", $0, s, m
}' pattern="$pattern" $*
NOTE:
Remember that an expression like [0-9]* will match
zero numbers (because * means
"zero or more of the preceding
character"). That expression can make
xgrep run for a very long time! The following
expression, which matches one or more digits, is probably what you
want instead:
xgrep "[0-9][0-9]*" files | wc -l
The xgrep shell script runs the
sed commands below, replacing
$re with the regular expression from the command
line and $x with a CTRL-b character (which is used
as a delimiter). We've shown the
sed commands numbered, like
5>; these are only for reference and
aren't part of the script:
1> \$x$re$x!d
2> s//$x&$x/g
3> s/[^$x]*$x//
4> s/$x[^$x]*$x/\
/g
5> s/$x.*//
Command 1 deletes all input lines that don't contain
a match. On the remaining lines (which do match), command 2 surrounds
the matching text with CTRL-b delimiter characters. Command 3 removes
all characters (including the first delimiter) before the first match
on a line. When there's more than one match on a
line, command 4 breaks the multiple matches onto separate lines.
Command 5 removes the last delimiter, and any text after it, from
every output line.
Greg Ubben revised showmatch and wrote
xgrep.
--JP, DD, andTOR
 |  |  |
32.16. Getting Regular Expressions Right |  | 32.18. Limiting the Extent of a Match |