Yep, I forgot to test :-(
Think I fixed the bug - Here ya go.
For testing I made the following file.
# cat /tmp/error_test.txt
this is line 1 error
this is line 2
this is line 3
this is line 4
this is line 5 error
this is line 6
Here is the new script:
#!/usr/bin/ksh
FILE_NAME=/tmp/error_test.txt # modify this and put your file name here.
# if you always have to specify the file name,
# the line above can be removed from this script
# if you provide the file name as an argument
# the default file name above will not be used.
if [ ${#1} -gt 0 ]
then
FILE_NAME="$1"
fi
TEXT="error" # text you want to look for
PRINT_NEXT_LINE="false"
cat $FILE_NAME|while read LINE # loop that reads every line of log file
do
if [ "$PRINT_NEXT_LINE" = "true" ]
then
echo "$LINE" # this the 2nd line
PRINT_NEXT_LINE="false"
else
TEST=`echo $LINE|grep -i "$TEXT"`
if [ ${#TEST} -gt 0 ]
then
FOUND_ONE="true"
echo "$LINE" # this is the first line
PRINT_NEXT_LINE="true"
else
FOUND_ONE="false"
fi
fi
done
This is the output from the script
# ./test
this is line 1 error
this is line 2
this is line 5 error
this is line 6
Robert
Robert G. Jordan
Robert@JORDAN200.com
Unix Admin, United Airlines