Search a pattern in a log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a pattern in a log file
# 1  
Old 09-29-2010
Search a pattern in a log file

I have file which gets updated every minute/second. Is it possible in shell scripting that I can search for some pattern infinitely in this file and if it finds that pattern, alert the user.

A sample of log file is below. The following file is getting updated every second. I want to alert the admin if it finds a "logout" each time. In simple words a kind of monitoring.

Code:
1234:login
234566:login
123456:logout

# 2  
Old 09-29-2010
Something like this (untested, run with nohup in background):

Code:
tail -f <your_file> |
  while IFS= read -r; do
    case $REPLY in
      ( *your pattern* ) run_your_commands ;;
    esac
  done

This User Gave Thanks to radoulov For This Post:
# 3  
Old 09-29-2010
You can try this:
Code:
nohup tail -f file | awk '/logout/{system("echo message | write root")}' &

It will run in the background until someone kills it or reboots the server (it will wun after you log out from console session too).
This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. UNIX for Dummies Questions & Answers

Search pattern in File

Hi I want to search pattern in file "S12345678B" which start with S and end with B total length is 10,numeric part 12345678 is variable. e.g S99001229B and S88226901B. please help (2 Replies)
Discussion started by: ashfaque
2 Replies

3. Shell Programming and Scripting

How to search pattern in file?

Hi I want to search pattern in file "S12345678B" which start with S and end with B total length is 10,numeric part 12345678 is variable. e.g S99001229B and S88226901B. please help (4 Replies)
Discussion started by: ashfaque
4 Replies

4. Shell Programming and Scripting

Script to search for a pattern in 30 minutes from a log file

Hello All, I have to write a script which will search for diffrent patterns like "Struck" "Out of Memory" , etc from a log file in Linux box's. Now I will be executing a cron job to find out the results by executing the script once in every 30 minutes. suppose time is 14-04-29:05:31:09 So I... (3 Replies)
Discussion started by: Shubhasis Mathr
3 Replies

5. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

pattern search for multiple log files and counting

I have 10 appservers and each appserver has 4 jvms . Each of these logs is archived and stored on a nfs directory . For example the files are /logs/200907/ap1-jvm1.server.log.20090715.gz /logs/200907/ap2-jvm2.server.log.20090714.gz /logs/200908/ap1-jvm1.server.log.20090812.gz I want to... (3 Replies)
Discussion started by: gubbu
3 Replies

8. Solaris

Search date pattern in application log file

I am viewing a file in vi editor and would like to search for a date pattern. In the log, the timestamp is enclosed in parentheses ''. I am using the '/' option in vi to search for the pattern. log snippet: 000000f4 ServletWrappe I SRVE0242I: : Initialization successful. 000000f4... (3 Replies)
Discussion started by: Vangogh78
3 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question