GNU's grep has the option --only-matching
, which prints just the matching region of a regular expression. I'm on a Solaris 5.10 box without any GNU tools installed, and I'm trying to achieve the same thing. Example:
grep -Eo "[0-9]+ ms" *.log
Is there a sed or awk expression that can do the same thing?
A possible refinement for versions of
sed
which support this type of alternation:In OS X, I had to use extended regular expressions because I couldn't get basic enhanced regular expressions to work (this would work in GNU
sed
, too, if you change-E
to-r
, but the basic enhanced version works there, too):These two latter examples work even if the sequence you're searching for appears at the beginning of the line with no leading characters.
Try using /usr/sfw/bin/ggrep
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
Solaris 5.10 includes bash 3.00, which has a modern regex engine. The follow command works, though it's quite a bit more verbose than a grep or sed solution.
(Formatted on multiple lines for readability)