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

  • Home
  • SEARCH
  • 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 8215301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:44:18+00:00 2026-06-07T11:44:18+00:00

I’m writing a Perl script and I need to capture some lines from a

  • 0

I’m writing a Perl script and I need to capture some lines from a garbage collection log and write them to a file.

The log is located on a remote host and I’m connecting using the Net::OpenSSH module.

I need to read the latest log file available.

In the shell I can locate the latest log with the following commands:

cd builds/5.7.1/5.7.1.126WRF_B/jboss-4.2.3/bin
ls -lat | grep '.log$' | tail -1

Which will return the latest log:

-rw-r--r--   1 load     other    2406173 Jul 11 11:53 18156.stdout.log

So in Perl I’d like to be able write something that locates and opens that log for reading.

When I have that log file, I want to print all lines that have a timestamp greater than a specified time. The specified timestamp is a $Runtime variable subtracted from the latest log message time.

Here are the last messages of the garbage collection log:

                                      ...

73868.629: [GC [PSYoungGen: 941984K->14720K(985216K)] 2118109K->1191269K(3065984K), 0.2593295 secs] [Times: user=0.62 sys=0.00, real=0.26 secs]
73873.053: [GC [PSYoungGen: 945582K->12162K(989248K)] 2122231K->1189934K(3070016K), 0.2329005 secs] [Times: user=0.60 sys=0.01, real=0.23 secs]

So if $Runtime had a value of 120 seconds, I would need to print all the lines from timestamp (73873.053 – 120) seconds up.

In the end my script would look something like this…

open GARB, ">", "./report/archive/test-$now/GC.txt" or die "Unable to create file: $!";

my $ssh2 = Net::OpenSSH->(
  $pathHost,
  user => $pathUser,
  password => $pathPassword
);
$ssh2->error and die "Couldn't establish SSH connection: ". $ssh2->error; 

# Something to find and open the log file.
print GARB #Something to return certain lines.
close GARB;

I realize this is somewhat similar to this question, but I can’t think of a way to tailor it to what I’m looking for. Any help is greatly appreciated!

  • 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. Editorial Team
    Editorial Team
    2026-06-07T11:44:20+00:00Added an answer on June 7, 2026 at 11:44 am

    Here’s an untested approach. I’ve not used Net::OpenSSH so there might be better ways to do it. I’m not even sure it works. What does work is the parsing part which I have tested.

    use strict; use warnings;
    use Net::OpenSSH;
    
    my $Runtime = 120;
    my $now = time;
    open my $garb, '>', 
      "./report/archive/test-$now/GC.txt" or die "Unable to create file: $!";
    my $ssh2 = Net::OpenSSH->(
    $pathHost,
      user => $pathUser,
      password => $pathPassword
    );
    $ssh2->error and die "Couldn't establish SSH connection: ". $ssh2->error;   
    
    # Something to find and open the log file.
    my $fileCapture = $ssh2->capture(
      q~ls -lat builds/5.7.1/5.7.1.126WRF_B/jboss-4.2.3/bin |grep '.log$' |tail -1~
    );
    $fileCapture =~ m/\s(.+?)$/; # Look for the file name
    my $filename = $1;           # And save it in $filename
    
    # Find the time of the last log line 
    my $latestTimeCapture = $ssh2->capture(
      "tail -n 1 builds/5.7.1/5.7.1.126WRF_B/jboss-4.2.3/bin/$filename");
    $latestTimeCapture =~ m/^([\d\.]+):/;
    my $logTime = $1 - $Runtime;
    
    my ($in, $out, $pid) = $ssh2->open2(
      "builds/5.7.1/5.7.1.126WRF_B/jboss-4.2.3/bin/$filename");
    while (<$in>) {
      # Something to return certain lines.
      if (m/^([\d\.]+):/ && $1 > $logTime) {
        print $garb $_; # Assume the \n is still in there
      }
    }
    
    waitpid($pid);
    
    print $garb;
    close $garb;
    

    It uses your ls line to look up the file with the capture method. It then opens a pipe through the SSH tunnel to read that file. $in is a filehandle to that pipe which we can read.

    Since we are going to process the file line by line, starting at the top, we need to first grab the last line to get the last timestamp. That is done with tail and, again, the capture method.

    Once we have that, we read from the pipe line by line. This now is a simple regex (the same used above). Grab the timestamp and compare it to the time we have set earlier (minus the 120 seconds). If it is higher, print the line to the output filehandle.

    The docs say we have to use waitpid on the $pid returned from $ssh2->open2 so it reaps the subprocess, so we do that before closing our output file.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.