Month: May 2010

Using Vim to Count Patterns

The other day, I posted a trick on using Vim with flat files.  Well, today, I wanted to count the number of times certain data conditions appeared in my file.  I created statistics on my program to show the count, but I wanted to check them to make sure the code was right.  I found this command would do the trick in Vim:

:%s/<pattern>//n

The “s” is the part that does a search and replace, but the “n” tells it to only count the matches rather than replace it with anything.  So, if I wanted to count the number of lines with “0” in the 123rd position, I would use this:

:%/^.\{122}0//n

Here is where I found the trick:

Wiki Technology – Vim: Count number of matches of a pattern

Extracurricular at Oracle

I came across a link to Team Oracle the other day.  I didn’t realize that they have their own Air Show team!  I’ll have to watch the schedule to see if I can catch them in our area.

While we are on the topic, I did already know that Oracle has their own Yacht Racing team.  You can check out BMW Oracle Racing here.

Now, we just need a Basketball team!  We could have Oracle Arena and the mascot could be the CPU Chips.  Maybe that is too much imagination.

Evaluate in SQR

I did a quick little test on the Evaluate syntax in SQR.  I can’t ever remember how the break statement works and if it is required.  So, here is the test —

Here is the code:

  move 1 to #test
  while #test < 6
    show 'Test = ' #test
    evaluate #test
      when = 1
      when = 2
        show 'evaluated as 1 or 2'
      when = 3
        show 'evaluated as 3'
      when = 4
        show 'evaluated as 4'
      when-other
        show 'evaluated as other'
    end-evaluate
    add 1 to #test
  end-while

Here is the output:

Test = 1.000000
evaluated as 1 or 2
Test = 2.000000
evaluated as 1 or 2
Test = 3.000000
evaluated as 3
Test = 4.000000
evaluated as 4
Test = 5.000000
evaluated as other

Vim Search in Flat Files

I have been working with Flat Files recently, and I found Vim Search patterns can help with finding certain conditions within the file.  This pattern works very well:

\{n}

Searches for n number of the preceding pattern.

For example, say you want to find any lines in the file that have a “2” in the 51st position of the file.  You can use this pattern:

^.\{50}2

So, this matches 50 characters of anything and a “2” following.

Reference:

Softpanorama: Vim Regular Expressions