Tanya Lattner | 4913cdc | 2004-11-13 21:03:22 +0000 | [diff] [blame] | 1 | #!/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 | # |
| 11 | set pattern [lindex $argv 0] |
| 12 | set num [lindex $argv 1] |
| 13 | |
| 14 | |
| 15 | # |
| 16 | # Get all of the lines in the file. |
| 17 | # |
| 18 | set lines [split [read stdin] \n] |
| 19 | |
| 20 | set index 0 |
| 21 | foreach 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 | } |