blob: 5ab0854b0b3f3216c6b6c266a886405888c48f3c [file] [log] [blame]
Tanya Lattner4913cdc2004-11-13 21:03:22 +00001#!/usr/bin/tclsh
2#
3# Usage:
4# prcontext <pattern> <# lines of context>
5# (for platforms that don't have grep -C)
6
7
8#
9# Get the arguments
10#
11set pattern [lindex $argv 0]
12set num [lindex $argv 1]
13
14
15#
16# Get all of the lines in the file.
17#
18set lines [split [read stdin] \n]
19
20set index 0
21foreach line $lines {
22 if { [regexp $pattern $line match matchline] } {
23 if { [ expr [expr $index - $num] < 0 ] } {
24 set bottom 0
25 } else {
26 set bottom [expr $index - $num]
27 }
28 set endLineNum [ expr [expr $index + $num] + 1]
29 while {$bottom < $endLineNum} {
30 set output [lindex $lines $bottom]
31 puts $output
32 set bottom [expr $bottom + 1]
33 }
34 }
35 set index [expr $index + 1]
36}