Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Printing th entire line with a keyword

by Anonymous Monk
on Dec 31, 2012 at 04:53 UTC ( #1010989=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I have an input file with multiple lines.The code is to search for a particular word in the line and to print the entire line if the word is present.Currently i am using a code as shown below

use strict; my $find = "word or string to find"; open FILE, "<searchfile.txt"; my @line = <FILE>; print "Lined that matched $find\n"; for (@lines) { if ($_ =~ /$find/) { print "$_\n"; } }

But here we are iterating through each line which is time consuming.Is there an easy method?

Comment on Printing th entire line with a keyword
Download Code
Re: Printing th entire line with a keyword
by Anonymous Monk on Dec 31, 2012 at 04:57 UTC

    But here we are iterating through each line which is time consuming.Is there an easy method?

    No. The only way to read something is to read it, there is no shortcut.

Re: Printing th entire line with a keyword
by roboticus (Canon) on Dec 31, 2012 at 05:11 UTC

    If you just want to print the lines containing a particular keyword, then the method you're using is probably the best: It's clear, easy to maintain, and unless you're doing a large amount of processing on very large files, is likely fast enough, to boot.

    However, it's possible to do so without reading the file line-by-line. You could do it this way:

    $ cat t.pl #!/usr/bin/perl use strict; use warnings; my $t; { # A: Slurp in the entire file local $/; $t = <DATA>; } # B: Scan the entire file for a lines containing either # of the words 'fox' or 'wood', and print them. + print "<$_>\n" for $t=~/^(.*?(?:fox|wood).*?)$/gm; __DATA__ Now is the time for all good men to come to the aid of their party. The quick red fox jumped over the lazy brown dog. How much wood can a woodchuck chuck? If a woodchuck could chuck wood? $ perl t.pl <party. The quick red fox jumped> <wood can a woodchuck chuck? If> <a woodchuck could chuck wood?>

    You'll want to read perldoc perlre for the details of the regular expression used. In it, I use two features that I only use rarely.

    ...roboticus

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

      Is there a way using grep?

      Because the corresponding code in shell script is like grep "searchkeyword" $filetobesearched> $outputfile

        It's even easier with grep. But in perl, grep wants to match against a list, so you'll be back to reading line-by-line:

        #!/usr/bin/perl use strict; use warnings print "<$_>\n" for grep {chomp; /(fox|wood)/} <DATA>; __DATA__ Now is the time for all good men to come to the aid of their party. The quick red fox jumped over the lazy brown dog. How much wood can a woodchuck chuck? If a woodchuck could chuck wood?

        This should give the same result. (Note: the chomp isn't required, but I wanted to match the output of the previous code, so I put it in.)

        ...roboticus

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

Re: Printing th entire line with a keyword
by RichardK (Priest) on Dec 31, 2012 at 11:25 UTC

    You could use the -n switch if you wanted a one line solution -

     perl -ne 'print if /key/' *.txt

    see perlrun for the full details.

Re: Printing th entire line with a keyword
by Anonymous Monk on Dec 31, 2012 at 13:13 UTC

    It's not really time consuming, but the way you wrote it is memory consuming since it slurps the whole file in the beginning. (And the memory consumption may make it slow if the file is very large.) We can do better by reading one line at a time:

    use strict; my $find = "word or string to find"; my $file; open $file, "<", "searchfile.txt"; print "Lined that matched $find\n"; while (<$file>) { if ($_ =~ /\Q$find\E/) { print "$_\n"; } }

    Note that I converted your open() syntax to the three-argument form, moved to non-bareword filehandles, and used escapes in the regexp. These are better programming practices.

    If you still want it faster, I don't think there's a better way than just using grep(1).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-07-12 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls