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.
-
A simple perl script to add to vim
a nice sequence string search.
-
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
-
The script can be operated on partial block of text from within
vim or on the entire file:
- Put marks, say ma and mb and type
:'a,'b !perl ~/bin/vim_find_consecutive.pl
-
:% !perl ~/bin/vim_find_consecutive.pl
The results, line numbers for match, are appended to the text file end.
-
The code is shown below:
- #!/bin/perl
- #:'a,'b !perl ~/bin/vim_find_consecutive.pl
- #:% !perl ~/bin/vim_find_consecutive.pl
- #line N+0 contains ARGV[0]
- #line N+1 contains ARGV[1]
- #line N+2 contains ARGV[2]
- #...
- $num_args = $#ARGV + 1;
- $line=0;
- $ptr=0;
- @a = ();
- while (<STDIN>) {
- chomp($_);
- print("$_\n");
- if($num_args > 0) {
- $ix=index($_, $ARGV[$ptr]);
- if($ix >= 0) {#match a member in the sequence
- $ptr++;
- if($ptr == $num_args) {#match of the entire sequence
- $ptr=0;
- push(@a, $line + 1 - $num_args);
- }
- } else { $ptr = 0; }
- }
- $line++;
- }
- foreach $j ( @a ) {
- print("$j\n");
- }
|