Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Get file part from the section where the matched pattern is found.

by rockstar99 (Novice)
on Nov 29, 2011 at 10:22 UTC ( #940571=perlquestion: print w/ replies, xml ) Need Help??
rockstar99 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to print a file from the point where matched pattern is found. I tried this
#!/usr/local/bin/perl my $file="show_configuration.out"; open (FILE, $file) or die "Can't open $file: $!\n"; select((select(FILE), $/ = undef)[0]); my $filecontents = <FILE>; close (FILE); if($filecontents =~ /\*\sdisks/){ my @matchsect = /\*\sdisks/; print "@matchsect\n\n"; } else{ print "No such pattern\n"; }
But not able to get it. Kindly let me know how to get the string from the matched pattern to EOF. Thanks in advance.

Comment on Get file part from the section where the matched pattern is found.
Download Code
Re: Get file part from the section where the matched pattern is found.
by choroba (Abbot) on Nov 29, 2011 at 10:26 UTC
    perl -ne '/\*\sdisks/ .. eof() and print'
Re: Get file part from the section where the matched pattern is found.
by pvaldes (Chaplain) on Nov 29, 2011 at 10:39 UTC
    if($filecontents =~ /(\*\sdisks)/){print $1,$'}
Re: Get file part from the section where the matched pattern is found.
by ansh batra (Friar) on Nov 29, 2011 at 10:49 UTC
    #! /usr/bin/perl -w use strict; open(FILE,"< file1.txt"); my @lines=<FILE>; close(FILE); my $found=0; foreach my $line(@lines) { if($found) { print "$line"; } if($line=~/start/) { $found=1; print "$&$'"; } }
    output
    startdexdbnjcrk this when be shown this will be shown this will also be shown
    contents of file were
    anjwdwus svwbsdncelf,c;rf wscgwhjdke ff ed iekd ewd asvdjcn startdexdbnjcrk this when be shown this will be shown this will also be shown
      Hi Ansh, Thanks for that code. How could I modify the code if I want to print
      startdexdbnjcrk this when be shown
      from the sample text you have chosen. Thanks in advance.

        if you meant that you want to print the line with the regex and the next line to it
        then simply add last; in the first if section

        if($found) { print "$line"; last; }
        this will get you the output
        startdexdbnjcrk this when be shown

        UPDATE:and if you want to print more lines then youll be needing a counter and check each time with "if" weather the desired number of lines have been printed or not. if yes then exit.


        there be many shortcuts but i guess this one is the simplest for a newbie like me to understand
Re: Get file part from the section where the matched pattern is found.
by Anonymous Monk on Nov 29, 2011 at 10:53 UTC
Re: Get file part from the section where the matched pattern is found.
by roboticus (Canon) on Nov 29, 2011 at 10:57 UTC

    rockstar99:

    Try changing this part:

    if($filecontents =~ /\*\sdisks/){ my @matchsect = /\*\sdisks/; print "@matchsect\n\n"; }

    to something like this:

    if ($filecontents =~ /\*\sdisks/) { # delete everything up to the first occurrence # of "* disks" $filecontents =~ s/^.*?\*\sdisks//; print "$filecontents\n\n"; }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Get file part from the section where the matched pattern is found.
by TJPride (Pilgrim) on Nov 29, 2011 at 12:54 UTC
    To give you exact code, I'd want to see your data. That often gives clues as to the best way to approach the problem. But here's a general solution:

    use strict; use warnings; { local $/ = undef; ### Set line terminator to nothing open(HANDLE, 'test.txt') || die; $_ = <HANDLE>; ### Slurp entire file } ### Match everything at or following first instance of pattern if (m/(my pattern.*)/s) { print $1; } else { print "No match found.\n"; }
Re: Get file part from the section where the matched pattern is found.
by Marshall (Prior) on Nov 30, 2011 at 16:06 UTC
    Something like this should do it (untested).
    If the pattern '* disks' can span multiple lines then the solution is more complex. But that looks like a typical set of tokens that would be found on a single line.
    #!/usr/bin/perl -w use strict; my $file="show_configuration.out"; open (IN, '<', $file) or die "Can't open $file: $!\n"; my $patternSeen =0; while (<IN>) { $patternSeen =1 if /\*\sdisks/; # '* disks' has been seen next unless $patternSeen; print; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940571]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-10-22 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls