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

The Archive Base Latest Questions

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

I have a log file which needs to be properly formatted into a readable

  • 0

I have a log file which needs to be properly formatted into a readable format. However the text file has no static number of lines or fixed primary values and has random number of spaces but has only a log file header which can be used to pin point the start and end of each time the application logs.

An Example of the log file:

Log File header
<text>
<text>
Log File header
<text>

After the script has been formatted it should look something like this:

Log File header
<text>
<text>

<space>

Log File header
<text>
<text>

Therefore I need some advice on greping out an entire paragraph everytime the Perl Script detects a “Log File header”.

Here is the grep perl script:

#!/usr/bin/perl

#use 5.010; # must be present to import the new 5.10 functions, notice 
#that it is 5.010 not 5.10

my $file = "/root/Desktop/Logfiles.log";
open LOG, $file or die "The file $file has the error of:\n =>  $!";

@lines = <LOG>;
close (LOG);

@array = grep(/Log File header/, @lines);

print @array;

Can someone please give some advice on the codes? Thanks.

  • 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-17T01:57:12+00:00Added an answer on May 17, 2026 at 1:57 am

    So you just want vertical space in between your log file sections?

    There are a few approaches, particularly because you know the header will be on a completely separate line. In all the following examples assume that @lines has already been populated from your input file.

    So first technique: insert spaces before header:

    foreach my $line ( @lines ) {
        if ( $line =~ m/Log File header/ ) {
            print( "\n\n\n" ); # or whatever you want <space> to be
        }
    
        print( $line );
    }
    

    The next technique is to use a regular expression to search/replace blocks of text:

    my $space = "\n\n\n"; # or whatever you want <space> to be
    my $everything = join( "", @lines );
    $everything =~ s/(Log File header.*?)(?=Log File header)/$1$space/sg;
    print( $everything );
    

    Some explanation about the regexp. The (?= means “look-ahead” which will match but not form part of the expression to be replaced. The /sg modifiers mean s-treat newlines as ordinary whitespace and g-do a global search-and-replace. The .*? means select anything, but as little as possible to satisfy the expression (non-greedy), which is extremely important in this application.

    update: edited first technique in which I’d failed to explicitly specify which variable to do the match upon.

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

Sidebar

Related Questions

I have a formatted string from a log file, which looks like: >>> a=test
i have a log file which needs to be written to the server .
I am working with a log file and I have a method which is
I have developed a web project. Which is generating log file using log4j. But
I have a log method which saves to a file that is named the
I have a log file that I am reading to a string public static
I have YAML file which needs to take a variable as an input: outputters:
I have a log file that I'm trying to append data to the end
I have a log file in SqlServer that stores the time an application started,
I have a log file containing statistics from different servers. I am separating the

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.