I have a (copy of) oracle listener log, and would like to replace the portnumber in every line. These are always of the form (PORT=1234) with sometimes 4 and sometimes 5 numbers.

So an exampleline, and I've anonimized it completely, but there are thousands of lines:

07-SEP-2020 06:34:05 * (CONNECT_DATA=(SERVICE_NAME=MYDB01)(CID=(PROGRAM=prog1@serv99)(HOST=client1283)(USER=john))) * (ADDRESS=(PROTOCOL=tcp)(HOST= * establish * MYDB01 * 0

I'd like to have that:

07-SEP-2020 06:34:05 * (CONNECT_DATA=(SERVICE_NAME=MYDB01)(CID=(PROGRAM=prog1@serv99)(HOST=client1283)(USER=john))) * (ADDRESS=(PROTOCOL=tcp)(HOST= * establish * MYDB01 * 0

You can use a :s command with a pattern that matches the port number and a replacement of xxxxx (or whatever you prefer.) Use a range of % to match the whole file.

When matching the pattern itself, a very useful feature of Vim regexes is \zs and \ze to mark the start and end of the match. That helps you match the characters that come before it (PORT=) or after ()) the digits, but only replace the digits themselves. (Without having to deal with regex look-ahead and look-behind zero-match syntax, or use match groups.)

So, in your particular case, you can use:


The \d\+ part matches one or more digits.


The easiest way to do it is to create a macro. That is a small recording of keystrokes that can be repeated a number of times. You would need to record keystrokes of the following steps. Macros are a powerful Vim feature worth learning.

  1. Start recording the macro by pressing qa. a would be the name of the macro.
  2. Search for the pattern PORT by using /. Press <Enter> to find the first occurrence.
  3. Press the lowercase e two times to move the cursor to the end of the = sign.
  4. Press w to move the cursor to the first number.
  5. Press ct) that means change until ) which will put you in insert mode. That will work on 4 or 5 or any character until it gets to the ).
  6. Type the x character five times and press <ESC>.
  7. Search for the next occurrence of PORT with n.
  8. Stop the recording by pressing q.
  9. Press 10@a to run the macro 10 times.
  • Added some formatting. I prefer f= to ee as it’s less likely to break. But great answer, and welcome to Vi and Vim! – D. Ben Knoble Sep 8 '20 at 13:58
  • Nice. I like the f= because it brings you just to where you need to go. – default Sep 9 '20 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.