Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

A combined application for grep -n ; vim -q ; and perl evaluated regex

Posted by peeterjoot on September 11, 2009

Now that I’ve learned of how to use evaluated replacement expressions in perl it’s become my new favorite tool. Here’s today’s application, using it as a query engine to figure out all the calls of a particular function that I want to look at in the editor and probably modify.

I’m interested in editing a subset of the function calls for the module in a given directory. I can find them and their line numbers with:

grep -n printIt.*BLAH *.C

But there’s 90 of these function calls, and I know most don’t need alteration. If I grep with context, say grabbing 20 lines of context after the search expression, I can see which of these are of interest:

grep -nA20 printIt.*BLAH *.C | tee grep.out

I really want to weed out all the calls that also do NOT contain additional expressions. Illustrating by example, a fragment of the grep output above had in it:

foo.C:6197:   printIt( BLAH,
foo.C-6198-          ...
foo.C-6200-          INFORMATIONAL,
foo.C-6205-          ...
foo.C-6210-          ) ;

Any of these calls that happen to have INFORMATIONAL or DUMPIT strings in them aren’t of interest, so I take my pre-canned evaluated regex perl script (see previous posts for an explaination) and modify it slightly.

This time I use:

# cat ./thisFilterScript
#!/usr/bin/perl

while (<>)
{
   $p .= $_ ;
}

$p =~ s/(printIt.*?;)/foo("$1")/smeg ;
print $p ;

exit ;

sub foo
{
   my $s = "@_" ;

   return "" if ( $s =~ /INFORMATIONAL/ or $s =~ /DUMPIT/ ) ;

   return "$s" ;
}

Run this on the grep output, and I’ve now reduced it to just a listing of the calls of interest:

# cat grep.out | ./thisFilterScript > grep.filtered

This is now just the filename:linenumber:output expressions for each of the function calls of interest.

# cat grep.filtered
foo.C:6303:         printIt( BLAH,
foo.C:6344:         printIt( BLAH,
foo.C:10298:   printIt( BLAH,
foo.C:10325:   printIt( BLAH,

I can now simply run ‘vim -q ./grep.filtered’, and I go straight to the line for the first hit (with :cn to get to the next when done with editing that call site).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: