Although there are better solutions as has already been suggested above, here is a quick script I just wrote to output 5 lines either side a line number you specify.
Just give it two arguments on the command line when you run it. 1. A line number you want to display, and 2. the file you want to display the line number from.
eg. lnb.pl 14 testfile.txt
This will display lines 9 through to 19 (5 either side of 14) from file testfile.txt
Enjoy!
Dean
#!perl
$nmb = shift;
$file = shift;
open (FILE, "<$file") or die $!;
@file = <FILE>;
close FILE;
print "\n";
foreach (@file) {
if (abs($nmb - ++$i) <= 5) {
print $i, ":\t$_";
}
}
print "\n";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.