Re: Seeking a grep-like script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10 June 2010 14:26, Bill Davidsen <davidsen@xxxxxxx> wrote:
> Timothy Murphy wrote:
>>
>> I'm looking for a grep-like script that searches for a given word,
>> and returns the paragraphs in which it appears (rather than the lines),
>> where a paragraph is defined as the material between 2 blank lines.
>>
>> All suggestions gratefully received.
>>
> It would be relatively easy to do in perl, suggested logic is to read in a
> paragraph, check for the word, print if found. Only takes a few lines of
> code.
>
> If by "looking for" you mean finding a script which does you what you want,
> I don't know where you find it, it's a bit trivial to bother to save.
>
> Sample attached.

The code becomes even easier if you put Perl into "paragraph mode".

#!/use/bin/perl

use strict;
use warnings;

$/ = ''; # paragraph mode - see "perldoc perlvar"

# Assume first argument is word to find
my $word = shift;

while (<>) {
  print if /\b\Q$word\b/; # \Q is a safety measure - escape metacharacters
}



Cheers,

Dave...
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines

[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux