1

Using the Ubuntu 12.04 and vim 2.22.0, why inside the vim I shall use:

:9s/\d\+/1/

to change first digit of the mentioned line to 1, but couldn't be able to use the same with sed as :

sed -i '9s/\d\+/1/' /home/file/foo

I still don't get any error neither any changes in the file. Do we have any inconsistency between the functionalities of vim and sed command ?

1 Answer 1

7

sed and vim are not related (well sort of, but it's far out). Their substitution commands are similar, but far from identical. Both of them use basic regular expressions, but vim has its extensions for it, and GNU sed has other extensions for it.

\d matches a digit in vim, in GNU sed it matches a d. To match a digit in sed, use [[:digit:]] or [0-9] (and those will also work in vim).

\+ is also an extension in both vim and GNU sed. The standard way to match one or more is to use \{1,\} instead.

4
  • tnx for the reply. but still now by using ":9s/[0-9]{1,\}/1/" it can not match the mentioned digit
    – Amir
    Jul 30, 2013 at 21:31
  • Simply I tried everything, but it didn't work out. do you have a clean answer buddy ?
    – Amir
    Jul 31, 2013 at 0:30
  • 1
    @Amir seems askubuntu decided to wrap that line after the first character. You're missing the backslash in front of {. sed '9s/[0-9]\{1,\}/1/'. You could also write it sed '9s/[0-9][0-9]*/1/'.
    – geirha
    Jul 31, 2013 at 5:39
  • Tnx @geirha for the answer;)
    – Amir
    Jul 31, 2013 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .