in reply to Print 4 Lines below matching criteria
Andas the one-liner it started out as (much love for -n and $_ in the right context):#!/usr/bin/perl use strict; use warnings; my $line; my $num_lines = 0; while ($line = <DATA>) { $num_lines = 4 if $line =~ /^id:/; if ($num_lines > 0) { print $line; $num_lines--; } } exit 0; __DATA__ John, Doe Michael id:1234567890123 library:ACME City, state:PUEBLO, CO Phone:719-555-555 Street:1610 Sorrow AVE APT D Zip: 81004 Jane, Doe Michael id:1234567890124 library:ACME City, state:PUEBLO, CO Phone:719-222-555 Street:1610 Happy AVE APT D Zip: 81006
perl -n -e '$n = 4 if /^id:/; if ($n) { print; $n--; }' your_data_file
|
|---|