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!
6 2994
Greetings.
Show us the code that you have so far.
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>;
@CTRC
For log files which fit into memory: -
open(FD, "<", $yourLogFile) or die "ERROR: $!";
-
-
@lines = <FD>;
-
-
close(FD);
-
-
@lastTwelveLines = splice(@lines, scalar(@file)-12, 12);
-
-
print @lastTwelveLines;
For huge log files, which would presumably not fit into memory: - open(FD, "<", $yourLogFile) or die "ERROR: $!";
-
-
while ( $line = <FD> ) {
-
push(@lastTwelveLines, $line);
-
-
@lastTwelveLines = splice(@lastTwelveLines, 1, 12) if ( scalar(@lastTwelveLines) > 12 );
-
}
-
-
close(FD);
-
-
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
Depending on what unix you are using maybe the system call "tail -n 11" will do what you want.
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 -
@lastTwelveLines = splice(@lines, scalar(@file)-12, 12);
-
Small syntax error in that line of code as @file does not exist. Fix and simplify to: -
@lastTwelveLines = splice(@lines, -12);
-
@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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |