Shell script to tail a file with unknown numbers

Hello,
I would like to write script to tail a file for different environment
But the number of lines are keep changing
How can I write a script
For example:
env could : A, B or C
and log files could be a.log, b.log and c.log
with the number of lines can change
say sometimes it 100 last lines or 400 last lines or full log files

How to accomplish this using a script

thanks in advance

Are the files, like a.log, always 100 lines?
Have you tried anything yet, like using the wc command to find the number of lines in a file? Please show us so we can help.

The ask to check number of files varies from time to time
and some time full logs are asked
so to put it in script in need to give an option with some 'read' syntax that it could get either 100000 or 200000 or full
so how to read that input which can vary

Assuming GNU tail you can either list lines from the end ( --lines=1234 ) or from the beginning ( --lines=+1234 ) of the file. Thus listing the full file could be done with

tail --lines=+1 myfile

In your script you could have a variable nlines which could contain the value 100000, 200000 or +1 and could be used thus:

tail --lines=$nlines $logfile

The best way to get the values into your script would be to use getopts as in

while getopts lhf name
do
   case $name in
   l) nlines=100000 ;;
   h) nlines=200000 ;;
   f) nlines=+1 ;;
   esac
done
shift $(( OPTIND - 1))

The log files could be added to the end of the argument list or with more option switches using getopts (use the bash help facility for more info).

Andrew

1 Like

is there a way to add an option for dynamic input
let's say I want to check for today's date or let's say I need to check for a word or let's say I need to check the number of lines
The thing is ask is dynamic so any input can be taken and applied to the search

Thanks

Be more precise with your queries. Check for a word in what? If I give you a word what do you want to do with it? When you check for today's date, do you mean find the first line with that date and then list every line from there to the end? Are you trying to say that sometimes you want to list the last n lines of the file and sometimes you want to grep it for a particular word? Or are you saying you are asked for random things and want to be ready for every possibility, including those you haven't thought of yet?

Andrew

yes please , asked for random things

for example:

tail < filename> | tail -100
tail < filename> | tail -200
tail < filename> | tail -500
cat < filename>  ( full file )
cat < filename> | grep "sept 10 2019"
cat <filename> | grep encrypt

so the ask could be anything number of lines, full file, date search, name search, etc
so any thing random

thanks a ton in advance

so the output goes to a file which needs to be sent to the requestor

tail < filename> | tail -100 > /tmp/log.a
tail < filename> | tail -200 > /tmp/log.b
tail < filename> | tail -500 > /tmp/log.c
cat < filename> ( full file ) > /tmp/log.e
cat < filename> | grep "sept 10 2019"> /tmp/log.f
cat <filename> | grep encrypt > /tmp/log.g

as this request comes few times in a day so if it can be automated though a script then don't have to do same thing again and again

Thanks a lot in advance

Hi,

You would just use somethin like this, but choose your logic to suit the most likely condition.

if [[ `grep "encrypt" /tmp/log.g` != "0" ]]
then
exit 0
else
mailx -s "Message Title." user@domain < /tmp/log.g
fi

You may want to build in checking or rotation on the logfile to ensure that you dont get the same notification time after time, this could be scripted to run as a cron job.

Regards

Gull04

Just for the fun of it - how far would this get you:

FL=$(wc -l < inputfile)
while read
  do    case $REPLY in
        [tT]*)  CMD="NR>$((FL - ${REPLY:1}))";;
        [pP]*)  CMD="/${REPLY:1}/";;
        [fF]*)  CMD=1;;
            *)  echo "usage: CMD Param; CMD ~ t)ail, p)attern search, f)ull"
                continue
        esac

        ((++CNT))
        awk "$CMD" inputfile > /tmp/log.$CNT
  done
2 Likes