ASIC/FPGA Design and Verification Out Source Services

Run a perl script from vim on a block of text, to find N strings in consecutive lines.

  1. A simple perl script to add to vim a nice sequence string search.

  2. Today I needed to find a location (line number in a text file), that contains a sequence of assembly commands. Actually I had a CPU command trace, which I needed to find its location in a disassembly list.
    So I want to find a lines where:
    line N   contains string 0
    line N+1 contains string 1
    line N+2 contains string 2
    ...
    line N+n contains string n


  3. The script can be operated on partial block of text from within vim or on the entire file:
    1. Put marks, say ma and mb and type
      :'a,'b !perl ~/bin/vim_find_consecutive.pl
    2. :%     !perl ~/bin/vim_find_consecutive.pl
    The results, line numbers for match, are appended to the text file end.


  4. The code is shown below:

    1. #!/bin/perl
    2. #:'a,'b !perl ~/bin/vim_find_consecutive.pl
    3. #:% !perl ~/bin/vim_find_consecutive.pl
    4. #line N+0 contains ARGV[0]
    5. #line N+1 contains ARGV[1]
    6. #line N+2 contains ARGV[2]
    7. #...
    8. $num_args = $#ARGV + 1;
    9. $line=0;
    10. $ptr=0;
    11. @a = ();
    12. while (<STDIN>) {
    13.   chomp($_);
    14.   print("$_\n");
    15.   if($num_args > 0) {
    16.     $ix=index($_, $ARGV[$ptr]);
    17.     if($ix >= 0) {#match a member in the sequence
    18.       $ptr++;
    19.       if($ptr == $num_args) {#match of the entire sequence
    20.         $ptr=0;
    21.         push(@a, $line + 1 - $num_args);
    22.       }
    23.     } else { $ptr = 0; }
    24.   }
    25.   $line++;
    26. }
    27. foreach $j ( @a ) {
    28.   print("$j\n");
    29. }


 


Also available on this project:


Home

Some tips for controlling the xterm title








Search This Site


Feedback This Site




new pages on this site