Saturday, December 22, 2007

Search String Within Paragraph and Print the Paragraph - UNIX

Here is another guest post by Mary M. Chaddock. She is a Network Security Administrator for a major university in Texas. Here is her time saving tip. Thanks Mary.

This little routine searches a file for a string and then prints the paragraph associated with that string.

Note: The Blogger application parsed out the greater-than and less-than characters so an underscore was used to preserve the contiguous nature of the <_string> <_filename> symbols.

Mary says…

Grep'ing from a file with more than one line per record:

You can do this with a Perl one-liner command: 'perl -00 -ne 'print if /'<_gstring>'/' <_filename>'

But I can rarely remember the Perl syntax, so I put this small shell script in my $HOME/bin directory:

# grep a string in a paragraph and print the paragraph.
# A paragraph is delimited by a blank line.
# syntax: gparagraph <_string> <_filename>
perl -00 -ne 'print if /'$gstring'/' $gfile

1 comment:

Anonymous said...

Hello from Spain.