1

Having a file with two feilds which is :(colon) delimited as shown below:

Ram:con_[3:4]_aen[52:7]
Rakesh:sync[0:128]

I need to convert the :(colon) using vim editor which is in brackets [] to ! as shown below :

Ram:con_[3!4]_aen[52!7]
Rakesh:sync[0!128]

I tried this but I am not getting the output as shown above.

:%s/[%d:%d]/[%d!%d]/g
1

You could use:

%s/\(\d\+\)\@<=:/!/g

\(\d\+\) Find a decimal number

\@<= Apply a positive look-behind. A positive look-behind means that the previous pattern ( in this case the one above ) is required to match on the next symbol. You can read more how VIM implements this here.

: Match on ':'.

0

You can use

:%s/\([[0-9]\+\):\([0-9]\+]\)/\1!\2/g

See that we have grouped the elements [number1 and number2] with \([[0-9]\+\) and \([0-9]\+]\) and reproduced the first with \1 and the second with \2, putting a ! in between them.

I have used [0-9]\+ to indicate one or more digits, but you could simplify to [0-9]* (which means zero or more digits) if you are sure that there is always a number between the colon and each bracket:

:%s/\([[0-9]*\):\([0-9]*]\)/\1!\2/g
1
  • Thank you so much its working
    – Kavitha
    Jun 15 '20 at 19:37
0
:%s/\v\(\[[0-9]+):([0-9]+\])/\1!\2/g

\v Enable extra regex features

(\[[0-9]+) capture group for opening bracket and at least one digit

: the colon you don’t want

([0-9]+\]) capture group for at least one digit and closing bracket

\1!\2 contents of the two capture groups with the bang you want between

1
  • thanks its working.
    – Kavitha
    Jun 15 '20 at 19:37

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.