The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl script to scan back lines gholdbhurg Shell Programming and Scripting 3 03-18-2008 12:33 PM
File Inventory Scan seacros Shell Programming and Scripting 4 03-17-2008 11:13 AM
File Scan Dastard Shell Programming and Scripting 2 04-08-2007 03:35 PM
How Can I Easily Determine If A File Has been Added to a Directory goodmis UNIX for Advanced & Expert Users 7 02-04-2007 01:13 AM
How Can I Easily Determine If A File Has been Added to a Directory goodmis UNIX for Dummies Questions & Answers 1 01-31-2007 06:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-28-2005
redlotus72 redlotus72 is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 69
How to scan only new lines added in file?

Hi,
I am planning to implement a scheduled script that will go against my log files (every hour), search for a set of key words (errors, exceptions, faults etc). The script must be intelligent enough to scan only the new lines added to the log file since it last ran.

I can use grep for searching, how can I scan only new lines added to the log file since it last ran?

Any idea? any sample script?
  #2 (permalink)  
Old 04-28-2005
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
You can find out how many lines are in the log file with wc -l. Use a second wc -l on the file to find out how many more lines were added - then just tail the difference. Save your last wc -l into a separate file for use on the next run

Run this command to start it all off (change the location and name of file to suit your needs)
wc -l logfile > /tmp/logfile.linecount

Get the number of lines from the linecount file (use cut or awk to get only the first field). Use that number and a new wc -l of the log file to find out how many new lines. Do your processing against only the new lines utilizing the tail command (see the man page). Save the new number of lines to your linecount file for the next run.
  #3 (permalink)  
Old 04-28-2005
VENC22's Avatar
VENC22 VENC22 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 15
Searching thru only new lines add

Here is the trick I think (Moderators please advise).

Use diff.
Suppose u r having file1 and file2. file2 is new and u want to search for occurence of LOTUS and red in new lines added in file2.
U can do this:

diff file1 file2 >> diff12
egrep 'LOTUS|red' diff12

This will work for single file.
If u have many files...U will have to right a shell script..
It will hae somewhat following structure.. (I have written algorithm so it won't be syntactically correct)

DO WHILE END OF NUMBER OF FILES.
integer i
string name
name = 'difffile' & string(i) & 'diff' --> if i = 4 then name = difffile4.diff
diff folder1/file1 folder2/file2 >> file2/name.diff
LOOP

this will give u a set of files with diferences in set of log files with only new lines.
(I am assuming that existing entries in a log file won't get updated or changed. E.g. if file1 contains line 1 as 'REDANDGREEN' then file 2 will contain line1 as REDANDGREEN and not REDANDORANGE. If the existing entries are going to get updated then u may need to process the files with differences again to keep only new line differences. Try out DIFF and try to investigate more.)
U can then fire a grep on these files with differences.

Let me know ur comments..
Thank
VEN
  #4 (permalink)  
Old 04-28-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
I have used the same solution as forwarded by RTM and it works very well that way ... the only thing is you might want to make sure your filecounter file is not in /tmp just in case of an unplanned reboot --- i put mine in /var/tmp which is not a volatile directory in my solaris setup ...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0