Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 139443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:26:09+00:00 2026-05-11T07:26:09+00:00

I have recently come up with a situation where I need to trim some

  • 0

I have recently come up with a situation where I need to trim some rather large log files once they grow beyond a certain size. Everything but the last 1000 lines in each file is disposed of, the job is run every half hour by cron. My solution was to simply run through the list of files, check size and trim if necessary.

for $file (@fileList) {   if ( ((-s $file) / (1024 * 1024)) > $CSize) {       open FH, '$file' or die 'Cannot open ${file}: $!\n';       $lineNo = 0;       my @tLines;        while(<FH>) {         push @tLines, $_;         shift @tLines if ++$lineNo < CLLimit;       }       close FH;        open FH, '>$file' or die 'Cannot write to ${file}: $!\n';       print FH @tLines;       close FH; } 

This works in the current form but there is a lot of overhead for large log files (especially the ones with 100_000+ lines) because of the need to read in each line and shift if necessary.

Is there any way I could read in just a portion of the file, e.g. in this instance I want to be able to access only the last ‘CLLimit’ lines. Since the script is being deployed on a system that has seen better days (think Celeron 700MHz with 64MB RAM) I am looking for a quicker alternative using Perl.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:26:10+00:00Added an answer on May 11, 2026 at 7:26 am

    I realize you’re wanting to use Perl, but if this is a UNIX system, why not use the ‘tail’ utility to do the trimming? You could do this in BASH with a very simple script:

    if [ `stat -f '%z' '$file'` -gt '$MAX_FILE_SIZE' ]; then     tail -1000 $file > $file.tmp     #copy and then rm to avoid inode problems     cp $file.tmp $file     rm $file.tmp fi 

    That being said, you would probably find this post very helpful if you’re set on using Perl for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm writing some code and have recently come across the need to implement
I have recently come across a situation where code is dynamically loading some libraries,
I recently have come across with a need for some type of translation-type that
I've recently come across the following situation. I have a UserControl that can show
I have recently come across an interesting question on strings. Suppose you are given
I have recently started learning c# and have come across an issue. I'm not
I have recently been working on a Pastebin script (for fun) and I've come
A few questions have come up recently involving the Application.Evaluate method callable from Excel
I have never come across this issue but most recently I noticed that a
My co-worker and I have come across this warning message a couple times recently.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.