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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:05:11+00:00 2026-06-09T20:05:11+00:00

I am trying to read a file and save the lines which starts with

  • 0

I am trying to read a file and save the lines which starts with $path till it encounters first balnk line in an array. I have the below code, bt it only prints the path name and not the lines. Could some-one have a look.

Below are the contents of the $file:

\sbd\archieve\date\form
-rwxrwxrwx 1 etd maadm 4354270 Aug 16 21:56 COMAHCUT.dat.20120816.ftpd.201208162156*
-rw-r--r-- 1 etd maadm       0 Aug 16 21:56 COMAHCUT.DONE.20120816.ftpd.201208162156

\sbd\single\archieve\date\form
-rwxr-xr-x 1 etd maadm 1362780 Aug 15 22:02 COMAINS.dat.ftpd.201208152203*
-rwxr-xr-x 1 etd maadm       0 Aug 15 22:02 COMAINS.DONE.ftpd.201208152203*

Below is the code i tried:
#!/usr/bin/perl

my $file = "/home/pauler/practice/DataIt/line.txt";
open (INFO, $file) or die "Cannot open the file $file :$! \n";
my $path = "\sbd\archieve\date\form";

foreach $line (<INFO>) {
        if ($line =~ m/$path/) {
        push (@array1, $line);
        last if ($line =~ m/^$/);
        print @array1;
        }
}
  • 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-09T20:05:12+00:00Added an answer on June 9, 2026 at 8:05 pm

    You can take advantage of the fact, that filehandles remember their position in the file.

    use strict;
    use warnings;
    
    my @array;
    my $path = '\sbd\archieve\date\form';
    
    while ( my $line = <DATA> ) {
        next unless $line =~ /\Q$path\E/;
        push @array, $line;
        while ( my $line = <DATA> ) {
            last if $line =~ /^\s*$/;
            push @array, $line;
        }
    }
    
    print @array;
    
    __DATA__
    \sbd\archieve\date\form
    -rwxrwxrwx 1 etd maadm 4354270 Aug 16 21:56 COMAHCUT.dat.20120816.ftpd.201208162156*
    -rw-r--r-- 1 etd maadm       0 Aug 16 21:56 COMAHCUT.DONE.20120816.ftpd.201208162156
    
    \sbd\single\archieve\date\form
    -rwxr-xr-x 1 etd maadm 1362780 Aug 15 22:02 COMAINS.dat.ftpd.201208152203*
    -rwxr-xr-x 1 etd maadm       0 Aug 15 22:02 COMAINS.DONE.ftpd.201208152203*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read a file which has several lines and print it
I am trying to save a file (and then read it later) in java
I am trying to read file and split its line to get some context(Computer
I'm trying to read a file line by line starting from a specific line
What I am trying to accomplish is to read a file line by line
I am trying to read file headers using java, I want to get file
I'm trying to read a file in my maven project at /src/main/resources/file.txt. I'm using
I am trying to read a file(temp.txt) placed under /root directory on remote host(ubuntu)
I am trying to read a file and then display the file in ASCII
I am trying to read from file: The file is multiline and basically i

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.