I sometime need to check some logs and I do this with this command:

egrep -o "success|error|fail" <filename> | sort | uniq -c

Sample input:

test error on line 10
test connect success
test insert success
test started at 00:00
test delete  fail

Sample output:

1 error
1 fail
2 success

I would like to know if someone knows a way to do this with a shorter command?

Before you ask why I would like to do this with an different command... No special reason, I'm just curious :)


Here is the awk way of doing it

awk 'BEGIN{RS=" "}/success/{s++}/fail/{f++}/error/{e++}END{print "Success:"s" Failed:"f" Error:"e}' abc

But all these one liners will be bit lengthier than our good old grep

| improve this answer | |

No, I think that you are as good as it gets. Naturally, you could do it with one perl script,

perl -nle  's/.*(error|fail|success).*/$1/ && $a{$_}++ ; END {  print "$_ $a{$_}" for keys %a } ' test.txt

...but it is more complex and less intuitive.

| improve this answer | |
  • I added a sample input to my question... Hope that now is more clear what the command do... sory for the inconvenience – Wolfy Sep 17 '12 at 7:56
  • 1
    OK, I modified the perl oneliner accordingly. I still think that your solution with egrep is better and more intuitive. – January Sep 17 '12 at 8:00

Not much shorter, but since you don't really need the regular expression, there's fgrep (grep -F).

fgrep 'success
fail' "$filename" | sort | uniq -c

another way to write the same thing in bash:

fgrep $'success\nerror\nfail' "$filename" | sort | uniq -c
| improve this answer | |

You could write a simple bash script and then call the script, like:

egrep -o "success|error|fail" "$1" | sort | uniq -c

and save it as (for example) myscript.sh. Then do a chmod +x myscript.sh and you can call it like myscript.sh <filename>.

| improve this answer | |
  • But in this case the command is the same... you just created a script that calls the command... – Wolfy Sep 17 '12 at 6:58
  • I thought you just wanted to speed up the grepping of your logs. Just curious, why do you want an alternative command if the one you already provided does a good job for you? – jeremija Sep 17 '12 at 7:54
  • like I said: I'm just curious how others do something like that... many times I realized that people know some fancy/nice way to do things and I love te learn now ways to do things... – Wolfy Sep 17 '12 at 8:01
  • 2
    In that case, you are that person :-) Your command line is nice, terse, clear and exploits the power of the Unix command line. What else to wish for? – January Sep 17 '12 at 8:14

Your command, while short and sweet, is a rather circuitous way to count occurrences of a term. I'd probably take the blunt, direct approach and use grep's -c flag (which does exactly that) inside of a shell loop:

for i in success test fail; do echo `grep -c $i <filename>` $i; done

Not as short, not as exciting, potentially faster for large logfiles (no sort). I'd say it's a wash.

| improve this answer | |

This could be a dummy answer but I think, in this case, sort is quite useless; maybe you can omit it. Nevertheless here we are using three different commands for three different actions.

We can short it if some of them con be reached with some option of grep, but I don't see which... :)

| improve this answer | |
  • 2
    Nope, it's not, otherwise uniq will count only consecutive instances of search terms. E.g. if you have success, success, fail, success, then uniq's output will be 2 success 1 fail 1 success. – January Sep 17 '12 at 8:03
  • that's ok... sry – Alessio Tomelleri Sep 17 '12 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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