summaryrefslogtreecommitdiff
path: root/test/Scripts/prcontext.tcl
blob: 5ab0854b0b3f3216c6b6c266a886405888c48f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/tclsh
#
# Usage:
# prcontext <pattern> <# lines of context>
# (for platforms that don't have grep -C)


#
# Get the arguments
#
set pattern [lindex $argv 0]
set num [lindex $argv 1]


#
# Get all of the lines in the file.
#
set lines [split [read stdin] \n]

set index 0
foreach line $lines {
    if { [regexp $pattern $line match matchline] } {
        if { [ expr [expr $index - $num] < 0 ] } {
            set bottom 0
        } else {
            set bottom [expr $index - $num]
        }
        set endLineNum [ expr [expr $index + $num] + 1]
        while {$bottom < $endLineNum} {
            set output [lindex $lines $bottom]
            puts $output
            set bottom [expr $bottom + 1]
        }
    }
    set index [expr $index + 1]
}