Smart multiline grep?

Jason Clinton me at jasonclinton.com
Tue Jun 21 11:32:19 CDT 2005


On Tuesday 21 June 2005 09:49, Gerald Combs wrote:
> Is there a grep-like utility that will return a variable number of
> context lines?  I need to search through source code for occurrences of

The problem statement is modeled by a fairly simple 5 state machine. Here's a 
very simple Ruby implementation of the state machine. It takes one argument 
which is the name of the function and searchable input on STDIN for grep-like 
behavior. It only returns the entire function header lines. Case sensitive, 
of course.

#!/usr/bin/ruby

more_state = false

while $stdin.gets()
    if  more_state == false and $_.include?( ARGV[0] )
        puts $_
        more_state = true if ! ( $_.include?("{") or $_.include?(";") )
    elsif more_state == true
        puts $_
        more_state = false if ( $_.include?("{") or $_.include?(";") )
    end
end

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://kclug.org/pipermail/kclug/attachments/20050621/cb1bcd17/attachment.pgp


More information about the Kclug mailing list