473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pulling Data From Log Files and Create a New Log File

1
Hello, I'm running into a wall when trying to figure out the Perl code to complete the following.

I need run a perl script that opens up a server log file (lets call this file SERVER_LOG.txt, located at c:\logs) and then copy the last 11 lines of that file and then print those 11 lines into a new log file (lets call it FINAL_LOG.txt)

I can open a new log file and write to it, but my problem is opening the current log file and copying the last 11 lines. I would greatly appreciate any code that might help me complete this!! Thank you for your time and help!
Feb 8 '07 #1
6 2994
miller
1,089 Expert 1GB
Greetings.

Show us the code that you have so far.
Feb 8 '07 #2
I'm not experienced in this, but maybe you could reverse the file, and then reed the first 11 lines.

Something like this:
print REV_FILE reverse <FILE>;
Feb 8 '07 #3
@CTRC

For log files which fit into memory:
Expand|Select|Wrap|Line Numbers
  1. open(FD, "<", $yourLogFile) or die "ERROR: $!";
  2.  
  3. @lines = <FD>;
  4.  
  5. close(FD);
  6.  
  7. @lastTwelveLines = splice(@lines, scalar(@file)-12, 12);
  8.  
  9. print @lastTwelveLines;
For huge log files, which would presumably not fit into memory:
Expand|Select|Wrap|Line Numbers
  1. open(FD, "<", $yourLogFile) or die "ERROR: $!";
  2.  
  3. while ( $line = <FD> ) {
  4.   push(@lastTwelveLines, $line);
  5.  
  6.   @lastTwelveLines = splice(@lastTwelveLines, 1, 12) if ( scalar(@lastTwelveLines) > 12 );
  7. }
  8.  
  9. close(FD);
  10.  
  11. print @lastTwelveLines;
@miller

For what do you need to see code in order to answer the question of CTRC??? I prefer to give a simple answer to a simple question ;o)

Greetz, Doc
Feb 9 '07 #4
jkr
3
Depending on what unix you are using maybe the system call "tail -n 11" will do what you want.
Feb 9 '07 #5
KevinADC
4,059 Expert 2GB
there are also modules for efficiently reading files backwards. You could also get the number of lines in the file and use the $. variable to get only the last 11. But if the file is changing constantly that might not work. You can also use Tie::File and simply read the tied array in reverse.

http://perldoc.perl.org/Tie/File.html
Feb 9 '07 #6
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. @lastTwelveLines = splice(@lines, scalar(@file)-12, 12);
  2.  
Small syntax error in that line of code as @file does not exist. Fix and simplify to:

Expand|Select|Wrap|Line Numbers
  1. @lastTwelveLines = splice(@lines, -12);
  2.  
@miller

For what do you need to see code in order to answer the question of CTRC??? I prefer to give a simple answer to a simple question ;o)

Greetz, Doc
Yes, his question was easy. However, he said that he had already created code and gotten stuck, and as a first time user I wanted to encourage him to share what he already had in order for us to be able to more directly diagnose where the block in his logic or experience was.

Nevertheless, you've helped him out regardless. Thanks for sharing your expertice.
Feb 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
2
by: Rudy | last post by:
Hello all! I have a web application, that I can upload files to a folder from the web site. The file path is stored in SQL. I have a 73GB drive, external hooked into the web server. I can load up...
4
by: washoetech | last post by:
Hello, I am working on a project where I need to be able to grab the data from an Excel spreadsheet and create a new table in my database based on the columns in the spreadsheet. After the...
1
by: Usarian Skiff | last post by:
I'm pulling a list of data from an excel file. When I open Excel files directly, if someone else has it open, I can select 'READ-ONLY' from a pop-up. When accessing the file from within my...
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
4
by: | last post by:
Hello, I have a client that is feeding PDF files to me. They do not want me to link to their files since that can put a strain on their bandwidth. What they asked I do is download the file and...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.