I have a file having lines as:


I want to convert string "_reg_number" into [number] at only last of every line in vi editor.
output should be:


I tried:


But it gives:


how to do it?


In command mode:


Here we use \+ to match one or more group of numbers at the end. So we don't have substitute with lines like cad/pqr_reg_.

  • Note this doesn't catch multiple digits, like for example 91. For the rest, it is the same as my answer :) – fedorqui Jul 28 '14 at 12:30
  • Downvoting: for posting almost the same as me and updating on grace period what I mentioned without mentioning, which made the solution even more close to mine. Wouldn't expect such behaviour from a veteran user. Also, an explanation of the command would help. – fedorqui Jul 30 '14 at 10:10
  • @fedorqui: Please checking again, your solution is different with mine. You can add an entry like cad/pqr_reg_, then yours will provide the wrong answer. – cuonglm Jul 30 '14 at 10:13
  • Yes, that is true and now I notice. But your initial answer (before my comment) was something like _reg_\([0-9]\)\+$, (good idea, but not working). I didn't like the situation. Could you do a fake edit so that I can undownvote? – fedorqui Jul 30 '14 at 10:26
  • @fedorqui: Maybe it's a simple mistypo, vim regex syntax is incredible, and I still editting while you was seeing my answer. And I don't think voting is important, do what ever you want:) – cuonglm Jul 30 '14 at 10:29

Just use this:


That is, catch the number with \( and \) and then print it back with \1. Note also there is no need to /g, because you perform it just once per line.

  • reg_ should be reg in your answer , BTW thanks :) – yogendra singh Jul 28 '14 at 12:35
  • Ok now I see, @yogendrasingh . Updated. – fedorqui Jul 28 '14 at 12:56

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.