looking for string in logfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting looking for string in logfile
# 1  
Old 05-23-2011
looking for string in logfile

I have a shell script that used to look for a particular sting in the last line of a log file. Howeve this string now has moved to the 3rd or 4th line from bottom.

Can anyone point me to how i easiest chage this one to look within the last frew lines (5 or so) for the particular string rather than just the very last one.

Here's the code that's used:

Code:
while [ "${SUCCESS}" = "FAILED" ]
do
LOGINFO="`/usr/bin/cat "${LOGFILE_NBU}" | /usr/bin/nawk \
'BEGIN{
last_line="";
}
{ # Main
if ($0 != ""){
last_line=$0;
}
} # end of Main
END{
printf("%s",last_line);
}'`"

if [ "${LOGINFO}" != "" ]
then

SUCCESS="`echo "$LOGINFO" | /usr/bin/nawk \
'BEGIN{
tsys_success="";
success="";
fla_backup=0;
fla_success=0;
number=0;
m=0;
}
{ # main
number=(split($0,arr00,""));
for (m=1; m <= number; m++){
if (index(arr00[m],"User_")>0){
fla_backup=1;
}

if (index(arr00[m],"essfull")>0){
fla_success=1;
}
if ((fla_backup==1) && (fla_success==1)){

success="OK";
printf("%s",success);
}
else{
success="FAILED";
printf("%s",success);
}
} # for loop
}'`" # end of main
fi

if [ ${i} -lt $LEFT_TIME ]
then
echo "+ + + + INFO :`/usr/bin/date`: Left time(secs) = $LEFT_TIME, WAITED_TIME(secs)=$i !"
echo "+ + + + INFO :`/usr/bin/date`: We are still waiting for the right information in the NBULOGFILE before we could proceed!"
/usr/bin/sleep 60
i=`expr $i + 60`
else
break
fi

done



Thanks for helping me.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-23-2011 at 08:24 AM.. Reason: code tags, please!
# 2  
Old 05-23-2011
Look at the command 'tail':
Code:
tail -n20 $logFile

will print last 20 lines of $logFile.
# 3  
Old 05-23-2011
Thanks for the quick reply.

tail -n would be fine, but when you look at the code there are some clauses to it.

How would I implement it within this code sniplet?
# 4  
Old 05-23-2011
Code:
tail -5 log_file.txt|while read line
do
    echo $line | # your nawk goes here and 
done

Or you could store the last few lines in temp file and run nawk on it
# 5  
Old 05-23-2011
Quote:
Originally Posted by kumaran_5555
Code:
tail -5 log_file.txt|while read line
do
    echo $line | # your nawk goes here and 
done

Or you could store the last few lines in temp file and run nawk on it
Hi kumaran_5555,

if i understand you correctly here, you would be replacing the whole of:

while [ "${SUCCESS}" = "FAILED" ]
do
LOGINFO="`/usr/bin/cat "${LOGFILE_NBU}" | /usr/bin/nawk \
'BEGIN{
last_line="";
}
{ # Main
if ($0 != ""){
last_line=$0;
}
} # end of Main
END{
printf("%s",last_line);
}'`"

if [ "${LOGINFO}" != "" ]
then


with just:

tail -5 log_file.txt|while read line
do
echo $line | /usr/bin/nawk \
'BEGIN{
tsys_success="";
success="";
fla_backup=0;
fla_success=0;
number=0;
m=0;
}
{ # main
number=(split($0,arr00,""));
for (m=1; m <= number; m++){
if (index(arr00[m],"User_")>0){
fla_backup=1;
}

if (index(arr00[m],"essfull")>0){
fla_success=1;
}
if ((fla_backup==1) && (fla_success==1)){

success="OK";
printf("%s",success);
}
else{
success="FAILED";
printf("%s",success);
}
} # for loop
}'`" # end of main
fi
I am lost here...?
Thanks for giving me a heads up
# 6  
Old 05-23-2011
Please use code tags!

no need for the while loop. Pipe it directly
Code:
tail -n5 logFile | nawk '{...}'

# 7  
Old 05-23-2011
Quote:
Originally Posted by mirni
Please use code tags!

no need for the while loop. Pipe it directly
Code:
tail -n5 logFile | nawk '{...}'

The code I opend the thread with is part of a whole compex scipt that seems do been written by somone Smilie dunno how to put this.
And I just try to change the code that is there with minimal invasion as possible to the whole thing as there is other code that would rely on the whole script to still work...and the whole thing has got a wrapper round with the loop that the script will look into the log file for 20hours Smilie to see if the string will occur. However as pointed out the string simply doesn't occure in the last line anymore so either the whole log file has to get grepped for or at least the last 5 lines...

Thanks for your great help and input!

---------- Post updated at 07:17 AM ---------- Previous update was at 06:40 AM ----------

I am coming up with the following which now should be doing the same but looking into the last 5 lines of the log file:



LEFT_TIME=`expr $CON_TIME_DIFF - $DIFF_TIME` # the time we would wait for success entry in nbulog!

if [ ${DIFF_TIME} -lt ${CON_TIME_DIFF} ]
then

i=0
SUCCESS="FAILED"

while [ "${SUCCESS}" = "FAILED" ]
do
LOGINFO="`/usr/bin/tail -n5 "${LOGFILE_NBU}" | /usr/bin/nawk \
'BEGIN{
last_line="";
}
{ # Main
if ($0 != ""){
last_line=$0;
}
} # end of Main
END{
printf("%s",last_line);
}'`"

if [ "${LOGINFO}" != "" ]
then

SUCCESS="`echo "$LOGINFO" | /usr/bin/nawk \
'BEGIN{
tsys_success="";
success="";
fla_backup=0;
fla_success=0;
number=0;
m=0;
}
{ # main
number=(split($0,arr00,""));
for (m=1; m <= number; m++){
if (index(arr00[m],"User_")>0){
fla_backup=1;
}

if (index(arr00[m],"essfull")>0){
fla_success=1;
}
if ((fla_backup==1) && (fla_success==1)){

success="OK";
printf("%s",success);
}
else{
success="FAILED";
printf("%s",success);
}
} # for loop
}'`" # end of main
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search specific string logfile specific date range

Hi, I have logfile like this.. === 2014-02-09 15:46:59,936 INFO RequestContext - URL: '/eyisp/sc/skins/EY/images/pickers/comboBoxPicker_Over.png', User-Agent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko': Unsupported with Accept-Encoding header === 2015-02-09... (8 Replies)
Discussion started by: kishk
8 Replies

2. Shell Programming and Scripting

Logfile monitoring with logfile replacement

Bonjour, I've wrote a script to monitor a logfile in realtime. It is working almost perfeclty except for two things. The script use the following technique : tail -fn0 $logfile | \ while read line ; do ... some stuff done First one, I'd like a way to end the monitoring script if a... (3 Replies)
Discussion started by: Warluck
3 Replies

3. Shell Programming and Scripting

Logs from logfile

Hi Team, Have to write a shell script to pick only 1 hr logs from the generated logfile and send it to other logfile. Thanks & Regards, Indu (3 Replies)
Discussion started by: indira_s
3 Replies

4. Shell Programming and Scripting

Reading string and adding the values in the final logfile

Dear all, I have small script which seems to be working but seems to have some bug. It suppose to read commonTxt and then print the noOfLines in outputFile. It is working for most of the txt but unable to add some of the variables values. Can somebody please spend looking at the thread and... (2 Replies)
Discussion started by: emily
2 Replies

5. Shell Programming and Scripting

Parsing Logfile

Hi, I need to continuously monitor a logfile to get the log information between a process start and end. the logfile look like this abcdddddddddd cjjckkkkkkkkkkkk abc : Process started aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbb abc... (6 Replies)
Discussion started by: Byorg
6 Replies

6. UNIX for Dummies Questions & Answers

writing to a logfile

I cannot get anything to go to my log. This is what i see on my screen when i run my korn shell. cd ok, cwd=/export/home/tsp_inst/TSP pget: /nas4/edata/tsp/rawdata/test2.gz: File exists But i cant get it to write it to my log. cd /this/is/my/newdirectory lftp -e 'pget -c -n 4... (1 Reply)
Discussion started by: tyngsboro
1 Replies

7. UNIX for Dummies Questions & Answers

change value of logfile

Hi, I have a log file like exyymmdd.log and i need to do is read the log file and inside the logfile if i find the word which starts with PQR002106570.book then i need to replace the value of PQRXXX.book(this is unique) with its corresponding title. A list of id(PQRXXX.book) and its title are... (5 Replies)
Discussion started by: umapearl
5 Replies

8. Shell Programming and Scripting

logfile parsing

I thought I was pretty handy with awk until I got this one. :) I'm trying to parse a log file where the events could have different delimiters (2 scripts is ok), the errors are spread over multiple lines, and I"m trying to figure out how to not read the same lines that have already been read. ... (1 Reply)
Discussion started by: linkslice
1 Replies

9. Shell Programming and Scripting

logfile

hi iam new of the ksh script.iwant in formation of how to call in logfile in ksh scripts. if the meaning in ksh. please help me thanks naveen.g (1 Reply)
Discussion started by: naveeng.81
1 Replies

10. Shell Programming and Scripting

last month's logfile

hi friends I need a shell script which will do the following Task Enter the month : if you enter 1 then it ll show you last 1 month's (starting from today).log file in the current directry. if you enter 4 then it ll show you last 4 month's (starting from today).log file in the current... (2 Replies)
Discussion started by: deep_kol
2 Replies
Login or Register to Ask a Question