Getting last 50 lines after finding a word

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Getting last 50 lines after finding a word
# 8  
Old 11-26-2009
tail -f EssbaseLog.txt
will run forever and will present lines as they are added to the file. Thus your script will run forever too. You run your script once and let it find errors as they occur.

You didn't use --follow=log-file but you may need it. Post the output from:
uname -a
so folks know what version of unix you have.
# 9  
Old 11-27-2009
Java

Code:
ReadLines=0
while [ true ]
do
TMPVAR=$(date +%s)
Totallines=$(wc -l /var/log/YOU.log )
Totallines=$((Totallines-ReadLines))
tail -n $Totallines /var/log/YOU.log | grep -A 50 'PATTERN' > /tmp/$TMPVAR.tmpfile
if grep 'username' /tmp/$TMPVAR.tmpfile ;
 then
   mail -s failure xxxxx@yyy.com < /tmp/$TMPVAR.tmpfile
fi
rm -rf /tmp/$TMPVAR.tmpfile
sleep 900
done

Some concept .. You need to just check with syntax to suit your system.

Basically what I am trying
While [ true ] ---> infinate loop
TMPVAR ... just to create tmp file...
Checking number of lines now and subtracting from what we have previously read. So that we dont want to send error which we have seen already ..
Tail -n ... read only last lines ... what we have not read from previous run .. And check patten and get lines matching above matching pattern.
Then again grep in the 50 lines from PATTERN which are in tmp file .. to see if username is available or not ..

if Available then mail .. else ... Nothing ..
Cleanup: remove all tmp file etc .. and sleep for 900 sec 15 mins ..

Better place to put this check is in cron:

Incase you decide to put in cron removing while do ... then push this number to some tmp file .. Like records read and etc ..
Also make note of more details in the same tmp where number of records are saved.. details like log file creation date etc to reset ReadLines variables incase the log gets rotated .. truncated etc ...


When you change PATTERN and username for your system .. Please add proper escape character wherever applicable
# 10  
Old 12-01-2009
1. Use crontab to run every fifteen minutes, key "man crontab" in shell.

2. This script may work <script.sh <your text to grep>:

#!/usr/bin/ksh

thelog="/your/local/logpath"
user=$1
lines="50"

run () {

num1=$(cat -n $thelog |grep $1 |awk '{print $1}' |head -1)
num2=$(expr $num1 - $lines)


if [ $num1 -gt 50 ]
then
results=$(cat -n $thelog |sed -n "$num2,${num1}p")

else
results=$(cat -n $thelog |sed -n '1,50p')

fi
echo "$results" |mailx -s "blabla" yourmailbox@nospamplease.com
}


run $user;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding a word through substring in a file

I have a text file that has some data like: PADHOGOA1 IOP055_VINREG5_1 ( .IO(VINREG5_1), .MONI(), .MON_D(px_IOP055_VINREG5_1_MON_D), .R0T(px_IOP054_VINREG5_0_R0T), .IO1() ); PADV30MA0 IOP056_VOUT3_IN ( .IO(VOUT3_IN), .V30M(px_IOP056_VOUT3_IN_V30M)); PADV30MA0 IOP057_VOUT3_OUT (... (2 Replies)
Discussion started by: utkarshkhanna44
2 Replies

2. Shell Programming and Scripting

Finding a word with awk or sed

Hello, in a AIX system : AIX CDRATE01 2 7 00FAB3114C00 my following commande give the result : LISTE /tmp/RESS **************************************************************** Liste TYPE = XXXXXXX EX = YYYY VER ... (13 Replies)
Discussion started by: sam01
13 Replies

3. Shell Programming and Scripting

Finding total count of a word.

i want to find the no:of occurrences of a word in a file cat 1.txt unix script unix script unix script unix script unix script unix script unix script unix script unix unix script unix script unix script now i want to find , how many times 'unix' was occurred please help me thanks... (6 Replies)
Discussion started by: mahesh1987
6 Replies

4. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

5. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

6. Shell Programming and Scripting

Finding a specific word

Hi, I am trying to develop a script which should find a word if a particular word exists. Below is the content of the file. insert_job: test_job ----> job name days_of_week: all start_times: "16:00" date_conditions: 1 insert_job: test_job2 ----> job name days_of_week: all... (16 Replies)
Discussion started by: rpatty
16 Replies

7. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

8. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

9. Shell Programming and Scripting

need help with finding a word in file's contents

Hi, I need to check if a particular name is already in the file or not and i am using following code for this... match=$(grep -n -e "$output1" outputfiles.txt ) where output1 is the variable name having names in it and outputfiles.txt is the file name ..and i am using ksh can anybosy... (6 Replies)
Discussion started by: manmeet
6 Replies

10. Shell Programming and Scripting

Finding a word in a file

Hi frndz, i have a flat file like, xxx yyy zzz sss aaa bbb yyy xxx rrr sss ttt yyy ddd zzzz cccc.. look, in this file i want to fetch the substring from one yyy to another one and need to print it then from next values between yyy's.. can you please give me some inputs on this.. ... (10 Replies)
Discussion started by: smr_rashmy
10 Replies
Login or Register to Ask a Question