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?

link|improve this question

It would help if you showed what a line from the file looks like and what exactly you're trying to extract. – Dennis Williamson Feb 10 '10 at 23:32
feedback

3 Answers

up vote 1 down vote accepted
sed -n 's/.*\([0-9]\+ ms\).*/\1/p' *.log
link|improve this answer
Thanks. Changing the + to * works. Solaris' sed doesn't support extended regular expressions. – brianegge Feb 11 '10 at 1:30
feedback

Try using /usr/sfw/bin/ggrep

/usr/sfw/bin/ggrep -V grep (GNU grep) 2.5

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.

>

link|improve this answer
1  
Nope. The sfw packages aren't installed, and I don't have access to install them. The only directory I can write to is /tmp, and it would be easier to copy the files back to a machine with gnu grep on it than to copy grep to /tmp. – brianegge Feb 11 '10 at 0:24
feedback

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.

cat *.log | while read line; 
do 
  if [[ $line =~ "([0-9]+) ms" ]]; then 
    echo "${BASH_REMATCH[1]}"; 
  fi;
done 

(Formatted on multiple lines for readability)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.