Thursday, February 27, 2014

vi Macro

Whenever I needed help with “vi” commands, I used to call SriRam . But he has moved to India. I am in trouble. I needed to know how to record and play macro, so I had to do Google search myself. This is so bad. Luckily I found following link and it was very helpful. This is what I needed to do

We process invoices using AP Invoice interface. In our test instance we needed to run same file multiple times. In order to process same file again and again we needed to change invoice number in the interface file. All invoice fields were separated by pipe character and invoice number was third field. To append “-A” (or something similar like '-B' or '-C') to each invoice number (to make it unique) I used following steps

  1. Open file in vi
  2. Go to first line and first character in the file
  3. Press q (it will start recording keystrokes or macro)
  4. Press a(you can press any character a to z to give a name to sequences of keystrokes)
  5. Press / ( to start search in vi)
  6. Press | and then return (to search pipe character)
  7. Press n (you will move to second pipe)
  8. Press n (you will move to third pipe)
  9. Press i to insert
  10. Enter –A
  11. Press ESC key
  12. Press enter (You will be at first character of second line)
  13. Press q to stop recording of keystrokes/macro

There were 45712 lines in the file, so by pressing following keys, all invoice numbers were suffixed with “-A”


First line was done while recording macro, so I needed to run macro only 45711 times

No comments:

Post a Comment