ASIC/FPGA Design and Verification Out Source Services
Run a perl script from vim on a block, to do complex text operations.
-
This page shows how to create a perl script, run it from within vim on a block of data to perform a complex text manipulation.
-
In this site this issue was already talked, as a side topic.
Today I had a list of more than 500 lines and I needed to extract all even lines into one specman list and the odd into another.
-
This was too much to do manually. A perl script did the job with as much as one iteration with relatively low effort. It saved me a lot of annoying keystroke typing and time, that I decided to dedicate a page for this nice vim feature on my web site.
The input was of this form:
0b0010111001
0b0010101110
...
0b0001011101
-
The output has to be in the following format:
var negative_code_l : list of uint(bits:10) = {
0b0010111001;
0b0010101110;
...
0b0001011101;
0b0001011110
};
var positive_code_l : list of uint(bits:10) = {
0b1101000110;
0b1101010001;
...
0b1110100010;
0b1110100001
};
-
The perl script has to use STDIN, to process data, which is piped from vim. It also has to buffer the data arriving from vim into two buffers: even and odd.
It than outputs the content of the buffers into the desired syntax. The script is shown below:
- #!/bin/perl
- $cnt=0;
- while (<STDIN>) {
- chomp($_);
- if($_ =~ /^ *(0b[0-9]*)/) {
- $buf=$1;
- if( ($cnt%2) == 0 ) {
- push(@n_a, $buf);
- } else {
- push(@p_a, $buf);
- }
- }
- $cnt++;
- }#while
- print(" negative_code_l : list of uint(bits:10) = {\n");
- foreach $j ( @n_a ) {
- print(" $j;\n");
- }
- print(" };\n");
- print(" positive_code_l : list of uint(bits:10) = {\n");
- foreach $j ( @p_a ) {
- print(" $j;\n");
- }
- print(" };\n");
To execute the script form within vim, type, after putting tags at the start and stop, to mark the block of text, that vim passes to the perl script:
:'a,'b !perl ~/bin/create_10Bpn_lists.pl
Block can be used relative to a single tab:
:'a+2,. !perl ~/bin/create_10Bpn_lists.pl
From current cursor to first empty line:
:.,/^$/-1!perl ~/bin/create_10Bpn_lists.pl
-
You may want to see this simple perl script, that puts
an incremental value into your text. Again this is
done by calling a script from within vim:
simple counter perl script
|