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 255725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:57:17+00:00 2026-05-11T21:57:17+00:00

I have a text file (basically an error log with date, timestamp and some

  • 0

I have a text file (basically an error log with date, timestamp and some data) in the following pattern:

mm/dd/yy 12:00:00:0001  
This is line 1
This is line 2

mm/dd/yy 12:00:00:0004  
This is line 3
This is line 4
This is line 5


mm/dd/yy 12:00:00:0004
This is line 6
This is line 7

I’m new at Perl and need to write a script that searches the file for timestamps and merges the data that have the same timestamp in it.

I’m expecting the following output for the above sample.

mm/dd/yy 12:00:00:0001  
This is line 1
This is line 2

mm/dd/yy 12:00:00:0004  
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7

What’s the best way to get this done?

  • 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-05-11T21:57:17+00:00Added an answer on May 11, 2026 at 9:57 pm

    I’ve had to do this task before on some very large files and the timestamps did not come in order. I didn’t want to store it all in memory. I accomplished the task by using a three-pass solution:

    • Tag each input line with its timestamp and save in temp file
    • Sort the temp file with a fast sorter, like sort(1)
    • Turn the sorted file back into the starting format

    This was fast enough for my task where I could let it run while I went for a cup of coffee, but you might have to do something more fancy if you need the results really quickly.

    use strict;
    use warnings;
    use File::Temp qw(tempfile);
    
    my( $temp_fh, $temp_filename )  = tempfile( UNLINK => 1 );
    
    # read each line, tag with timestamp, and write to temp file
    # will sort and undo later.
    my $current_timestamp = '';
    LINE: while( <DATA> )
        {
        chomp;
    
        if( m|^\d\d/\d\d/\d\d \d\d:\d\d:\d\d:\d\d\d\d$| ) # timestamp line
            {
            $current_timestamp = $_;
            next LINE;
            }
        elsif( m|\S| ) # line with non-whitespace (not a "blank line")
            {
            print $temp_fh "[$current_timestamp] $_\n";
            }
        else # blank lines
            {
            next LINE;
            }
        }
    
    close $temp_fh;
    
    # sort the file by lines using some very fast sorter
    system( "sort", qw(-o sorted.txt), $temp_filename );
    
    # read the sorted file and turn back into starting format
    open my($in), "<", 'sorted.txt' or die "Could not read sorted.txt: $!";
    
    $current_timestamp = '';
    while( <$in> )
        {
        my( $timestamp, $line ) = m/\[(.*?)] (.*)/;
        if( $timestamp ne $current_timestamp )
            {
            $current_timestamp = $timestamp;
            print $/, $timestamp, $/;
            }
    
        print $line, $/;
        }
    
    unlink $temp_file, 'sorted.txt';
    
    __END__
    01/01/70 12:00:00:0004
    This is line 3
    This is line 4
    This is line 5
    
    01/01/70 12:00:00:0001
    This is line 1
    This is line 2
    
    
    01/01/70 12:00:00:0004
    This is line 6
    This is line 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can make those methods static and it will work,… May 12, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer My guess: make sure you're handling didReceiveMemoryWarning correctly. Put a… May 12, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer You want to put an attribute value on the innerHTML… May 12, 2026 at 7:28 pm

Related Questions

I'm having a problem when trying to pass an array back to a COM
So right now i need to create and implement an extension of the Python
I have C# code that cycles through .sql files and executes what's inside them
I have the following code in one of our projects webpages: XmlDocument xDoc =

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.