Help On tail script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help On tail script
# 1  
Old 02-16-2012
Help On tail script

Hi Guys,

I would like to create a script which tails the content of a log file in real time, looks for a specific string , like "ERROR" and captures in a text file the previous 10.000 lines that were existing before this string.


Any help is appreciated.
# 2  
Old 02-16-2012
This is not possible by tail command to predict when ERROR is coming and capture the 10 lines before that, instead of this, script can read the file in a loop every time where it left and parse the file up to next error and get the previous 10 line. let me get the some raw script for you.
# 3  
Old 02-16-2012
This could be the foundation of such a script
Code:
while true ; do 
  tail  $logfile | grep ERROR ||  tail -10000 $logfile > prev &&echo "Dumped log records to prev"&& break ; done

# 4  
Old 02-16-2012
Let me make it more clear for you.
I am using some application that puts all the logging under /var/adm/messages. Once i capture the string "Error" I need to find out what actually caused it, 1000 lines before and after, to have the full picture.Then put all these lines in a text file.
It's going to be something like this :

Code:
$logfile= /var/adm/messages
while true ; do    tail  $logfile | grep ERROR ||  tail -1000 $logfile  > prev file.txt &&echo "Dumped log records to prev"&& break ; done

---------- Post updated at 04:24 PM ---------- Previous update was at 03:03 PM ----------

Hi,

Is my above expression correct ?

Thanks!

Last edited by methyl; 02-16-2012 at 06:04 PM.. Reason: please use code tags
# 5  
Old 02-16-2012
The script posted contains too many syntax errors to make the intention clear.

What Operating System and version do you have and what Shell are you using?
# 6  
Old 02-17-2012
try this.
Code:
#!/bin/bash
previousSearch=0
while true;
do
        Lines=$(cat  /var/adm/messages | grep -n ERROR | cut -d ":" -f1)  #to get the all lines numbers which has word ERROR
        for i in $Lines
        do
                if [ $i -gt previousSearch ]; then
                line=$(expr $i + 1000)                             #get the maximun number of line which is needed
                head -$line | tail -2000 >> newfile.txt                   # get exact 2000 line, 1000 before ERROR and 1000 after ERROR
                previousSearch=$i                                         # setting counter to last search result.
                fi
        done
done

I was not able to run this but if there is any syntax error please fix thos, but logic seems to be good.

you run this script as background process and tail the newfile.txt to get the result.

---------- Post updated at 10:44 AM ---------- Previous update was at 10:34 AM ----------

in fact you can use this line also instead of original

Lines=$(grep -n ERROR /var/adm/messages | cut -d ":" -f1) #to get the all lines numbers which has word ERROR

This would be must efficient then original "cat".

Last edited by vishal_vsh1; 02-17-2012 at 04:38 AM.. Reason: updating the syntax
# 7  
Old 02-17-2012
I'm still getting an error when running the script :

[: previousSearch: bad number
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to end tail -f from the script?

Hi, i have a script tail -f logs| grep item i need to end the script once it finds item in the logs folder, this happens on run time. (10 Replies)
Discussion started by: nikhil jain
10 Replies

2. Shell Programming and Scripting

Joining multiple files tail on tail

I have 250 files that have 16 columns each - all numbered as follows stat.1000, stat.1001, stat.1002, stat.1003....stat.1250. I would like to join all 250 of them together tail by tail as follows. For example stat.1000 a b c d e f stat.1001 g h i j k l So that my output... (2 Replies)
Discussion started by: kayak
2 Replies

3. Shell Programming and Scripting

script to kill tail processes

my unix machine is currently shared by many teams, because of that lots of processess are running and bad part is taht when I do psu ...i can see all tail processes as well , meaning ppl who have viewed files with tail and have forgotten to close it. command prompt >> psu tail -n 0 -f... (2 Replies)
Discussion started by: mitsyjohn
2 Replies

4. Shell Programming and Scripting

bash script to display tail

Hi everyone, I'm trying to write a script to format a file using unix2dos. I want to output all but the first 14 lines in a file. Then I want to pipe this to unix2dos to convert the output to a file that's easily readable on windows. Here's what I have: export Lines=`wc -l < $1` export... (11 Replies)
Discussion started by: LuminalZero
11 Replies

5. Shell Programming and Scripting

Doing a tail in a script and then return back and continue script

Hello all, I am trying to do a tail in a script. But when I quit the tail my script quits also. This is not what I want. I am struggling to get this done. #!/bin/bash askFile() { echo -n "Enter file: " read FILE } doTail() { tail -F "${1}" } askFile doTail... (4 Replies)
Discussion started by: markdark
4 Replies

6. Shell Programming and Scripting

using tail -f and tr in a script

I have file that is being constantly written to example: file.txt ABC EBC ZZZ ABC I am trying to create a simple script that will tail this file and at the same time using tr to change B to F on lines containing 'B'. I tried this and it doesn't seem to work. #!/bin/bash tail -f... (8 Replies)
Discussion started by: zerofire123
8 Replies

7. Shell Programming and Scripting

tail script gone wrong

Hi guys... What am I doing wrong? #!/bin/sh LINES=1 FILE=test.log TAIL="/usr/bin/tail -n" $TAIL -${LINES} ${FILE} Error: ./tail.sh usage: tail ] tail ] ---------- Post updated at 08:41 AM ---------- Previous update was at 08:32 AM ---------- please ignore this... (3 Replies)
Discussion started by: jazzaddict
3 Replies

8. Shell Programming and Scripting

How to tail log in one script?

I have three prodcution box Prod1 Prod2 Prod3 I want to write a script which tail the log for each production box and put it into some file which I have want to tail For example Prod1 ----- TIMER IXN=MEMPUT, USR=GGu1, elapsed = 0.176 seconds. 11:41:44 AUDIT MEMPUT: member... (4 Replies)
Discussion started by: mr_harish80
4 Replies

9. 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

10. 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
Login or Register to Ask a Question