TABLE OF CONTENTS
::pwtk::grep_field
SYNOPSIS
proc ::pwtk::grep_field {pattern file which_field {index ""}} {
PURPOSE
A "field-segmented" version of ::pwtk::grep command. It returns only a specified field from the lines that matches the pattern.
ARGUMENTS
- pattern -- pattern to grep for [pattern matching is that of Tcl's "string match" command]
- file -- file to grep
- which_field -- which field from the grepped line to return; its meaning/syntax is the same as for the Tcl 'lindex' command; 0 = first field; end = last field
- index -- [optional] which match to return; its meaning/syntax is the same as for the Tcl 'lindex' command; 0 = first match; end = last match
SOURCE
set result "" pwtk::lineread line $file { if { [string match *$pattern* $line] } { lappend result [lindex $line $which_field] } } if { $index != "" } { set result [lindex $result $index] } return $result }