Search file or log for words or strings


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search file or log for words or strings
# 1  
Old 11-11-2012
Search file or log for words or strings

i want to search a log for occurrences of words and i want the result to tell me how many lines in the log contained each word.

if i type a command like this:

Code:
egrep "cat|dog|monkey|bananas|bike" logfile

i would like a response like this:

Code:
cat=3,dog=17,monkey=1,bananas=102,bike=51

the numbers above for each word being searched for denotes the number of lines found in the log that contained those words.

OS: linux / sunos
language: bash
# 2  
Old 11-11-2012
Code:
# deliberate UUOC
for foo in dog cat mouse carcass
do
  cnt=$(cat *.log |  grep -c -F "$foo" )
  printf " $s=%d " $foo $cnt
done

printf "\n"
# 3  
Old 11-11-2012
Quote:
Originally Posted by jim mcnamara
Code:
# deliberate UUOC
for foo in dog cat mouse carcass
do
  cnt=$(cat *.log |  grep -c -F "$foo" )
  printf " $s=%d " $foo $cnt
done

printf "\n"
i dont understand this.

the section with the cat, doesn't that search in every log file that ends with ".log"? i want to search in a specific log file

and it loks like you're cating the log file several times. that cant be good. any alternative?
# 4  
Old 11-11-2012
Code:
# cat logfile
cat dog dog cat monkey
banana bike
dog

Code:
# cat logfile | tr ' ' '\n' | sort | uniq -c
   1 banana
   1 bike
   2 cat
   3 dog
   1 monkey

You can pipe the above output to grep to filter out the words that you are interested in.

Last edited by Yoda; 11-11-2012 at 03:46 PM..
# 5  
Old 11-11-2012
try also:
Code:
awk -v sw="cat|dog|monkey|bananas|bike" '
BEGIN { c=split(sw,a,"[|]"); }
{ for (w in a) { if ($0 ~ a[w]) d[a[w]]++; } }
END { for (i in a) { o=o (a[i]"="d[a[i]]","); }
  sub(",$","",o); print o;
}' logfile

This User Gave Thanks to rdrtx1 For This Post:
# 6  
Old 11-11-2012
rdrtx1, In my file which I posted above, I have 3 occurrence of word: dog and there are 2 occurrence of word: cat. But your script is showing only 2 & 1 respectively. Looks like occurrence in same line is not getting counted. Can you please recheck?
Code:
dog=2,monkey=1,bananas=,bike=1,cat=1

# 7  
Old 11-11-2012
from the original post:
Quote:
the numbers above for each word being searched for denotes the number of lines found in the log that contained those words.
This User Gave Thanks to rdrtx1 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search the multiple strings in app server.log?

Hi Team, Could you please suggest the below requirement. using the below server log, i just want only the order_id with combination of customer name , error code value as 5000111 & amount value using the shell script i tired using the command grep "error_code>50001111"... (6 Replies)
Discussion started by: venkat918
6 Replies

2. Shell Programming and Scripting

Search and repllace of strings with space between words

Dear all, I have gone through all the search and replace requests but none of them meet my particular need. I have a huge file in which all Unicode characters are stored as Names. A sample is given below. I want to replace strings in that file with a mapper from another file termed as master.dic. ... (4 Replies)
Discussion started by: gimley
4 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search for words NOT in a text file

I have a long list of alphanumberic words (no spaces or characters) in file1.txt I need to check for the existance of each of the words from file1.txt against file2.txt and if the word is NOT in file2.txt, I need to know about it, either standard output or redirect to file3.txt For example:... (5 Replies)
Discussion started by: ajp7701
5 Replies

6. Red Hat

monitoring search strings /var/log/messages

Hello Everyone, I am setting up monitoring system for linux server, I need to add few some common error message in monitoring file which can search those strings in /var/log/messages file on the client. can someone suggest few error messages. Thanks, (2 Replies)
Discussion started by: bobby320
2 Replies

7. Shell Programming and Scripting

How to search and append words in a file

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958 only in... (2 Replies)
Discussion started by: gsreeni
2 Replies

8. UNIX for Dummies Questions & Answers

search words in different file

Hi, I have 1 - 100 file I want the list of such file which contains word 'internet' Please provide command to do this (3 Replies)
Discussion started by: kaushik02018
3 Replies

9. UNIX for Dummies Questions & Answers

Search File for Specific Words

I have a file that contains the following: Mon Dec 3 15:52:57 PST 2o007: FAILED TO PROCESSED FILE 200712030790881200.TXT - exit code=107 Tue Dec 4 09:08:57 PST 2007: FAILED TO PROCESSED FILE 200712030790879200a.TXT - exit code=107 This file also has a lot more stuff since it is a log file.... (2 Replies)
Discussion started by: mevasquez
2 Replies

10. Shell Programming and Scripting

search for words in file

hi all, i would like to search in a directory. all files they were found shoul be opend and looked about a keyword. if keyword is found i want to see the name of the file. i've rtfm of find and have a command like this : find /etc -exec cat \{}\ | grep KEYWORD but don't work, and : find... (4 Replies)
Discussion started by: Agent_Orange
4 Replies
Login or Register to Ask a Question