bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:
in Perl. This basically prints the previous and the next line when the word 'hello' is found. I've done a super search and came up with the following links:grep -A1 -B1 'hello' file_name
Thanks in advance.#!/usr/bin/perl use strict; my ($prev, $next); while (my $line = <DATA>) { if ( $next ) { print "$line"; $next = undef; } if ( $line =~ /hello/ ) { print "$prev" if defined( $prev ); print "$line"; $next++; } $prev = $line; } __DATA__ test1 test2 hello test3 test4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Equivalent of Linux Grep Command
by Corion (Patriarch) on Jul 13, 2009 at 19:37 UTC | |
|
Re: Equivalent of Linux Grep Command
by thunders (Priest) on Jul 13, 2009 at 19:42 UTC | |
|
Re: Equivalent of Linux Grep Command
by CountZero (Bishop) on Jul 13, 2009 at 21:19 UTC | |
|
Re: Equivalent of Linux Grep Command
by Your Mother (Archbishop) on Jul 13, 2009 at 21:26 UTC | |
|
Re: Equivalent of Linux Grep Command
by JavaFan (Canon) on Jul 13, 2009 at 20:41 UTC | |
|
Re: Equivalent of Linux Grep Command
by ambrus (Abbot) on Jul 13, 2009 at 23:10 UTC | |
|
Re: Equivalent of Linux Grep Command
by petdance (Parson) on Jul 18, 2009 at 06:45 UTC |