agreen54 has asked for the wisdom of the Perl Monks concerning the following question:

can any one post me the sample code to get lines from a particular file after matching a line

sample file output hostname 06-NOV-13 log status Status of the log 1 248641 2 211749 3 246008 4 262190 5 175450 I want to get all lines after status of the log to end of the line a +nd assign to a array

Replies are listed 'Best First'.
Re: get few lines from a file
by hdb (Monsignor) on Nov 11, 2013 at 08:50 UTC

    Skip, then slurp:

    use strict; use warnings; while( <DATA> ){ last if /^Status of the log/; } my @lines = do { local $/; <DATA> }; print @lines; __DATA__ hostname 06-NOV-13 log status Status of the log 1 248641 2 211749 3 246008 4 262190 5 175450
Re: get few lines from a file
by NetWallah (Canon) on Nov 11, 2013 at 07:36 UTC
    perl -ne 'push @arr,$_ if m/^Status of the log/..undef}{print for @ar +r' Your-log-file-name.here
    (ab)uses the flip-flop operator. See "Range operator" in http://perldoc.perl.org/perlop.html.

                 When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

      Hi,

      Thanks for the code. I need small modification. I need to remove that status of the log line and print rest of the lines below it

        It is against the spirit of this site to request someone to write code for you without any attempt on your part, because the main purpose is education, which usually requires effort on the part of the recipient.

        That said, here is some code that will do the job, but I'm hoping you will try to understand how it works, and if you have difficulty with that, please post specific questions explaining how much you have understood, and what part you have difficulty with.

        perl -ne 'push @arr,$_ if $x;$x||=m/^Status of the log/}{print for @a +rr' Your-file.name
        Note - the only reason the code contains a "push @arr" is because you originally requested the contents be placed in an array.

                     When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Re: get few lines from a file
by CountZero (Bishop) on Nov 11, 2013 at 08:38 UTC
    I want to get all lines after status of the log to end of the line
    Do you mean perhaps "end of the file"?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: get few lines from a file
by Laurent_R (Canon) on Nov 11, 2013 at 16:07 UTC

    Or start to print as soon as you meet the desired input.

    my $print_this = 0; while (<$IN>) { print if $print_this; $print_this = 1 if /^Status of the log/; }