Script to retrieve only recently added records from a file

Shell scripting can be leveraged to accomplish very critical tasks. If you are a administrator then in your day to day tasks you can use small shell scripts to automate few of your daily tasks and save time and energy. Here we had a requirement of reading a log file entries for error messages from a perticular time to time as we wanted to check for any new error entries in log file. So we will discuss about shell script get most recent record and process it.

Below is a simple shell script which can be used to check a file for any new entries added after last run of the script. Below shell script keeps the line number recorded in a file in local folder and checks for the new entries after that line till end of the file. It is done through a while loop where you can add additional tasks or calculations on the new entries as per your requirement like here we are only grepping for any new ERROR messages so that we can analyse and keep a tab on what the application is doing.

There are many examples of reading a file from line x to y and reading new entries from a file, below is one of them. I have added the links to different sites where you can also read more on similar examples.

 

#!/bin/bash
# Script to read only recently added records/entries from a file

#######################
#Variables Section
#######################

LogFileName=$1
CountFileName=${LogFileName}_Count
CCount=`cat $LogFileName | wc -l`
OldCount=`cat $CountFileName`

if [[ -z "$OldCount" ]]; then
export OldCount="0"
fi;

while read line; do

# DO Your Calculations here
echo $line | grep ERROR
done < <(tail -n "+$OldCount" $LogFileName)

echo $CCount >$CountFileName

 

you can just redirect the output and send it to the administrator too using the mailx command too.

Below are few more examples on web for your reference.

read contents of a file from a given line number

read line from a file by passing line number

Read lines starting from a line number in a bash script

cat line X to line Y on a huge file

 

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.