Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
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

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. – Feb 25 '12 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .