Operating System - Linux
1755639 Members
3356 Online
108837 Solutions
New Discussion юеВ

Searching Errors in a log file

 
SOLVED
Go to solution
James R. Ferguson
Acclaimed Contributor

Re: Searching Errors in a log file

Hi Rory:

I did not mean any offense; rather only that Perl has some of the best regular expression support available. Your use of '[:space:]' enables whitespace (a space or a tab) detection and is good defensive technique. This works:

# awk '{if (tolower($0) ~/[[:space:]]error|[[:space:]]warn/) {print $0}}' filename

Note the double square brackets. This is necessary to create a character class consisting of '[:space:]'.

Regards!

...JRF...
Rory R Hammond
Trusted Contributor

Re: Searching Errors in a log file

Jim,

No offense was taken and I hope my reply did not suggest that I was offended. The discussion about problems and solutions are always good.


Rory
There are a 100 ways to do things and 97 of them are right
Shivkumar
Super Advisor

Re: Searching Errors in a log file

Hi John,

Is this the right syntax to use your script ?

$scriptname.sh "Error" "5" "logfilename"
Shivkumar
Super Advisor

Re: Searching Errors in a log file

I was trying to execute the "john korterman's" script. I got the error message.

./tlog: syntax error at line 25: `TOTAL_LINES=$' unexpected

There appears to be some syntax error in the line:-
TOTAL_LINES=$(awk '{nlines = nlines + 1}' END {print nlines} <$3)

=====================================================================
Below is the complete script.

script name:tlog

#!/usr/bin/sh
# Print number of lines, $2,
# after matched , case-insensitive string, $1,
# from file, $3
#

typeset -i FIRST_LINE=0 LAST_LINES=0 MATCH_LINE_NO=0 LINE_AFTER=0 TOTAL_LINES=0


# Check params..
if [ "$#" != 3 ]
then
echo wrong number of parameters
echo par1=string, par2=number of lines, par3=file
exit 1
fi
#
if [ ! -r $3 ]
then
echo Cannot read $3
exit 1
fi

# Number of lines in $3
TOTAL_LINES=$(awk '{nlines = nlines + 1}' END {print nlines} <$3)



grep -in "$1" "$3" | while read LINE
do
# Line number for match
MATCH_LINE_NO=$(echo $LINE | awk -F: '{print $1}')

# Check line number boundaries
FIRST_LINE=$(($MATCH_LINE_NO))
#
LAST_LINE=$(($MATCH_LINE_NO + $2))
if [ "$LAST_LINE" -gt "$TOTAL_LINES" ]
then
echo match in line $MATCH_LINE_NO, but cannot print lines after $TOTAL_LINES
fi

# Print after...
LINE_AFTER=$(($MATCH_LINE_NO + $2))
cat -n $3 | awk -v lineb=$FIRST_LINE -v linea=$LINE_AFTER '$1==lineb,$1==linea {print $0}'
echo
done
====================================================================
Appreciate suggestion.

Thanks,
Shiv
Sandman!
Honored Contributor

Re: Searching Errors in a log file

You are missing the terminating single-quote for the awk construct. The single quote should be placed after the action specified by the END statement...imho change the line below:

from...
TOTAL_LINES=$(awk '{nlines = nlines + 1}' END {print nlines} <$3)
to...
TOTAL_LINES=$(awk '{nlines = nlines + 1} END {print nlines}' <$3)

~hope it helps
Shivkumar
Super Advisor

Re: Searching Errors in a log file

Hi Sandman,

now i am getting different error as:-
`MATCH_LINE_NO=$' unexpected

for the below line in the script:-

# Line number for match
MATCH_LINE_NO=$(echo $LINE | awk -F: '{print $1}')

James R. Ferguson
Acclaimed Contributor

Re: Searching Errors in a log file

Hi Shiv:

I'm not sure what you have. I copy-and-pasted John's script from your post of today (Dec 20). I corrected line-25 as Sandman pointed out and can run the script without errors and receive the output I expect.

I suggest you re-copy-and-paste and try the same.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Searching Errors in a log file

Hi Shiv,

I copied John's script and it runs perfectly on my machine. As JRF says there is a problem with the copy 'n paste on your end.

~cheers
Shivkumar
Super Advisor

Re: Searching Errors in a log file

Now getting new errors.

Here is the messages:-

1st test:

$./t1 "Errors" 5 logfile
awk: record `java.class.path = /o...' too long
record number 1653
match in line 1732, but cannot print lines after 0
awk: syntax error near line 1
awk: bailing out near line 1


2nd test:
$./t1 "listening on port 1000" 5 logfile

match in line 2013, but cannot print lines after 0
awk: syntax error near line 1
awk: bailing out near line 1

Looks like some awk syntax errors ??
James R. Ferguson
Acclaimed Contributor

Re: Searching Errors in a log file

Hi Shiv:

From your output, I would guess that the log file has a record (a string of characters delimited with a newline) that exceeds an 'awk' internal limit. You might find, too, that you can't 'vi' the file for the same reason. Perl has no practical limits.

Regards!

...JRF...