Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|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
add comment

3 Answers

up vote 2 down vote accepted
sed -n 's/.*\([0-9]\+ ms\).*/\1/p' *.log
share|improve this answer
    
Thanks. Changing the + to * works. Solaris' sed doesn't support extended regular expressions. –  brianegge Feb 11 '10 at 1:30
    
@brianegge: The equivalent to + when it isn't available is, in this case, [0-9][0-9]* –  Dennis Williamson Sep 18 '13 at 15:35
add comment

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.

>

share|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
add comment

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)

share|improve this answer
    
Bash regex handling changed for version 3.2. You should not quote the pattern because it will be treated as a literal string instead of a regex. Since unquoted patterns also work in Bash 3.0 and 3.1, you should use them there, too, for forward compatibility. Further, I recommend that the pattern be stored in a variable and the variable should be used on the right side of =~. For example: pattern='([0-9]+) ms'; if [[ $line =~ $pattern ]] –  Dennis Williamson Sep 18 '13 at 15:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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