Get count of multiple word in single command

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Get count of multiple word in single command
# 1  
Old 04-02-2017
Get count of multiple word in single command

Hello Experts,

I have a log file that contains 4 different type of exception :
1- Exception
2- Fatal
3- Error
4- Exec

My requirement is to find count of each type of exception, i tried using combination of -E and -C but that doesn't seems to be working :
Code:
grep -ec 'Exception' -ec 'Fatal' -ec 'Error' -ec 'Exec' log.txt

Any guess ow to do it?
# 2  
Old 04-02-2017
grep builds one overall pattern to match in the file(s) and thus can only yield one count. Use either four greps, one for every single keyword, or try
Code:
awk -vKEYS="Exception Fatal Error Exec" '
BEGIN   {for (MX=n=split(KEYS, T); n>0; n--) SRCH[T[n]]
        }
        {for (s in SRCH) if ($0 ~ s) CNT[s]++
        }
END     {for (c in CNT) print c, CNT[c]
        }
' file
Fatal 1
Exec 1
Error 1
Exception 1

This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-02-2017
Hi.

Using a pipeline grep -> sort -> uniq:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate count of 4 unique strings, grep, sort, uniq.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C grep sort uniq

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
grep -Eo "Exception|Fatal|Error|Exec" $FILE |
tee f1 |
sort |
tee f2 |
uniq -c

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
grep (GNU grep) 2.20
sort (GNU coreutils) 8.23
uniq (GNU coreutils) 8.23

-----
 Input data file data1:
Now is the time
This is an Exception
for all good men Exec
to come to the aid
Sparkle Farkle Error
of their country.
And now, this: Fatal
Exception, finally.

-----
 Results:
      1 Error
      2 Exception
      1 Exec
      1 Fatal

See files f1, f2 for intermediate data. See man pages for details.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 4  
Old 04-02-2017
Perl solution for a log file with highly improbable text in line # 9:
Code:
$ 
$ cat -n logfile.txt
     1    this is line # 1
     2    this is Exception line
     3    this is line # 3
     4    this is Fatal line
     5    this is line # 5
     6    this is Error line
     7    this is Exec line
     8    this is line # 8
     9    this is Error line and Exception line
    10    this is line # 10
    11    this is Exception line
$ 
$ perl -lne '$h{$1}++ while(/(Exception|Fatal|Error|Exec)/g)}{print "$v $k" while(($k,$v)=each %h)' logfile.txt
1 Exec
2 Error
3 Exception
1 Fatal
$ 
$


Last edited by durden_tyler; 04-02-2017 at 05:44 PM..
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 04-02-2017
Hi.

The grep->sort->uniq pipeline also works with that:
Code:
...
 Input data file data2:
this is line # 1
this is Exception line
this is line # 3
this is Fatal line
this is line # 5
this is Error line
this is Exec line
this is line # 8
this is Error line and Exception line
this is line # 10
this is Exception line

-----
 Results:
      2 Error
      3 Exception
      1 Exec
      1 Fatal

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count same word which has come many times in single lines & pars

can i get a simple script for , Count same word which has come many times in single lines & pars Eg file would be == "Thanks heman thanks thanks Thanks heman thanks man" So resullt should be Thanks = 5 heman=2 man = 1 thanks in advance :) Please use code tags for code and... (1 Reply)
Discussion started by: heman96
1 Replies

2. UNIX for Dummies Questions & Answers

Single UNIX command to display users and to count them

Hello everyone, I am new to Unix and I am stuck with a problem. I need only a single command to display the output of who and then add the total number of users and display at the bottom of that output. Example-: (Expected output) sreyan@debian:~$ <command> sreyan tty7 ... (7 Replies)
Discussion started by: sreyan32
7 Replies

3. Shell Programming and Scripting

running multiple command in a single line

Hi Can we run the linux command and per script in a single command $ cd /usr/local/adm/ ;ctsv scmtest_qabuild ;cspec.pl scmtest This is a combination of linux and clearcase command and last one is perl script with argument. I can see the first and 2nd coomand is executing but last... (6 Replies)
Discussion started by: anuragpgtgerman
6 Replies

4. Shell Programming and Scripting

Empty out multiple files with a single command?

I have a log directory: /logs/foo.log /logs/bar.log /logs/err.out I'm trying to find a way to > /logs/*.log > /logs/*.out to blank them out, but of course, that doesn't work. Any suggestions? (4 Replies)
Discussion started by: Validatorian
4 Replies

5. Shell Programming and Scripting

~~Unix command to count a particular word in the whole directory .~~

Hi , i'm trying to count a particular word occurance in a whole directory..is this possible :wall: say for example there is a directory with 100 files which and all the file may have the word 'aaa' in it ...how would i count the number of 'aaa' in those whole 100 files in a directory ? ... (10 Replies)
Discussion started by: Rabbitsfoot
10 Replies

6. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

7. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

8. UNIX for Dummies Questions & Answers

Word Count (WC) command

I have created a file "pat".This file contains the text "abcdefghi". Now i want to count the characters in this file "pat". I gave wc -c | pat and it returns me exact count "9". Now when i tried like "echo pat | wc -m" It returns '4'. as for as i understand this command "echo pat | wc -m" should... (7 Replies)
Discussion started by: mdali_vl
7 Replies

9. Shell Programming and Scripting

adding single word to multiple line.

I have following problem. <File A> contains let say 5 lines but can be changed. cat dog fish car if I want to add word to each line then how do I go about it? I used paste -d but that requires two files having same number of lines but in my case <File A> changes and I just need to... (6 Replies)
Discussion started by: paulds
6 Replies

10. UNIX for Dummies Questions & Answers

How to run multiple command in single command?

Dear Unix Guru, I have several directories as below /home/user/ dir1 dir2 dir3 Each directory has different size. I want to print each directory size (Solaris command du -hs .) Can you please guide me how to achieve this? Thanks Bala (2 Replies)
Discussion started by: baluchen
2 Replies
Login or Register to Ask a Question