Shell script to tail a file with unknown numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell script to tail a file with unknown numbers
# 1  
Old 09-10-2019
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
# 2  
Old 09-10-2019
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.
# 3  
Old 09-11-2019
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
# 4  
Old 09-12-2019
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
Code:
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:
Code:
tail --lines=$nlines $logfile

The best way to get the values into your script would be to use getopts as in
Code:
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
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 09-12-2019
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
# 6  
Old 09-12-2019
Quote:
Originally Posted by encrypt_decrypt
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
# 7  
Old 09-12-2019
yes please , asked for random things

for example:
Code:
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

Moderator's Comments:
Mod Comment
Use code and/or icode tags please.

Last edited by Peasant; 09-13-2019 at 08:01 AM.. Reason: Added code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

2. Shell Programming and Scripting

How to tail -f multi logfile in 1 shell script.?

Hello, How to tail -f multi logfile from multi path in 1 shell script. File & Path /usr/home/localmode/mode110l/log/logic/number110/digit110_digit110m4_2013050210.txt /usr/home/localmode/mode103l/log/logic/number103/digit103_digit103m4_2013050210.txt... (4 Replies)
Discussion started by: ooilinlove
4 Replies

3. Shell Programming and Scripting

script to tail file; problem with awk and special characters

Trying to use code that I found to send only new lines out of a log file by doing: while :; do temp=$(tail -1 logfile.out) awk "/$last/{p=1}p" logfile.out #pipe this to log analyzer program last="$temp" sleep 10 done Script works fine when logfile is basic text, but when it contains... (2 Replies)
Discussion started by: moo72moo
2 Replies

4. Shell Programming and Scripting

calculation using awk or shell script in between the numbers

file A E969K D223L E400L E34L file B predicted 3 1 250 251 500 501 1000 The output should be E969K 501 1000 D223L 1 250 E400L 251 500 E34L 1 250 I tried in this way (1 Reply)
Discussion started by: cdfd123
1 Replies

5. Shell Programming and Scripting

Shell script to check numbers!

Hello All, I have 3 types of files. The names of which starts with P,I,M like P********* D********* M********* now I need to do some operations witht hese files.. so if file name starts with P or p then do the operation for P file... fi else (20 Replies)
Discussion started by: smarty86
20 Replies

6. Shell Programming and Scripting

How do I get my script to monitor a new file using tail?

Hi, I have a script which basically watches a log file for new lines using tail, then takes action based on what is logged. I wrote a script to do this for me and its working great, my only problem is that once per week, this log file is archived to another directory, and a new log is created.... (4 Replies)
Discussion started by: lstorm2003
4 Replies

7. Shell Programming and Scripting

Grep or Tail in shell script

Hi, I am writing a shell script that checks catalina logs on a production system and mails me if it detects errors. It greps the logs for known errors which i have defined as variables. The problem is the logs are huge, approx 30,000 before they rotate. So I am forced to use grep instead... (3 Replies)
Discussion started by: Moxy
3 Replies

8. Shell Programming and Scripting

How do I feed numbers from awk(1) to tail(1)?

Hello, I am too daft to remember how to properly feed numbers that I've extracted with awk(1) to tail(1). The actual question is probably a lot more simple than the context, but let me give you the context anyway: I've just received some email that was sent with MS Outlook and arrived in... (8 Replies)
Discussion started by: ropers
8 Replies

9. Shell Programming and Scripting

add numbers in shell script

cat dailyreports | grep "Important list" | awk -F":" '{print $2}' | awk -F" " '{print $1}' hey guys, after running the above combination of cat and awk, i get the below output: 3 4 2 9 now, i need to add these numbers up all in one line. i dont know what to add to that cat and awk one... (2 Replies)
Discussion started by: Terrible
2 Replies

10. Shell Programming and Scripting

retain Line numbers.. in Vi .. OR .. A SHELL SCRIPT

Hello everybody ! GOT SOMETHING INTERESTING... I am trying to retain line number for a text document.. usually we get line numbers in VI using :set nu , but I want to permanently store them. It's a 4000 lines of text and I want grep/search it for a list of words/fields stored in a different... (2 Replies)
Discussion started by: sdlayeeq
2 Replies
Login or Register to Ask a Question