Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a file which has the below pattern

=================================================

4601 -- it means completed
4602 -- it means failed
4603 -- it means timed out

=================================================

If I do tail -10 to file, then I would get anyone pattern from above.

I want to grep above three number and then display message as Completed|Failed|Timed-Out.

Any Idea?

Thanks!

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Here's a small script that you can use for testing - run it somewhere safe, e.g. /tmp or so:

# Prepare
rm *.txt
echo 4601 -- it means completed > f1.txt
echo 4602 -- it means failed > f2.txt
echo 4603 -- it means timed out > f3.txt

# Test
for i in *.txt; do echo
  echo --- Processing file $i
  if grep -qs 4601 $i; then
    echo Completed
  elif grep -qs 4602 $i; then
    echo Failed
  elif grep -qs 4603 $i; then
    echo Timed-Out
  fi
done

Thanks @grawity for the helpful tip (see his comment below) for not creating unnecessary subshells / running tests / speeding it up here!

It will create 3 files - f1.txt, f2.txt and f3.txt, each containing one of the coded you mention and then process each of the files, generating the messages you want.

Hope this helps.

share|improve this answer
    
Thank you icyrock!! it worked...exactly what I neeeded. Cheers! –  maneeshshetty Feb 25 '12 at 5:50
    
if [ `grep -c ...` -gt 0 ] is unnecessary. if grep -qs ... is enough. –  grawity Feb 25 '12 at 13:43
    
@grawity Thanks, that's a nice tip - edited for others who might stumble upon. –  icyrock.com Feb 25 '12 at 15:49
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.