4

I have a large sql file and I would like to rename the table name from `feedback` to `comments` but I don't want to replace the word feedback in case any row has that text so I was trying to select it with the quotes but vim can't find it. I'm using

:s/`feedback`/`comments`/

Any idea how to do it? maybe vim is not te best option for this?

7
  • You should probably consider something like sed or perl, but vim will do. Shouldn't you use some SQL tool for this? How could you distinguish feedback as a table name from feedback as a word in content? Or does it come quoted using backticks?
    – muru
    Nov 7 '14 at 10:38
  • @muru The sql file has already the backticks. I have never use sed or perl. That's why I tried with Vim
    – SERPRO
    Nov 7 '14 at 10:41
  • If they are quoted using backticks, it should work. You can try using the c flag (:s/.../.../c) to make it confirm each change.
    – muru
    Nov 7 '14 at 10:45
  • @muru I tried that E486: Pattern not found: `feedback` The funny thing is that the word is highlighted now in the editor.
    – SERPRO
    Nov 7 '14 at 10:46
  • Did you escape the backticks? I could substitute if I used it as seen in the question, but if I escaped, I get the same error as you and the highlighting happened.
    – muru
    Nov 7 '14 at 10:49
6

While vim certainly is capable of doing this, my first thought would be to use sed or perl. Using sed:

sed -i.bak 's/`feedback`/`comments`/g' path/to/file

The -i flag makes sed perform operations in the files specified instead of writing to stdout (-isuffix makes a backup copy path/to/filesuffix).

1
  • 1
    It's also good to use -i.bak, so that the original file gets backed up to path/to/file.bak.
    – fedorqui
    Nov 7 '14 at 15:51
5

Try in vim:

:%s/`feedback`/`comments`/gc

this should work as you expected

3
  • Thanks for your answer That will do the trick too.. I need to check the difference between :s and :%s
    – SERPRO
    Nov 7 '14 at 11:01
  • @SERPRO % replaces in the entire file. It's an address range for the operation.
    – muru
    Nov 7 '14 at 11:02
  • please enter with the quotes. as you wanted
    – Ramya A
    Nov 7 '14 at 11:06
2

Here's a vi/vim one-liner (using ex -s to avoid opening the full screen editor resulting in a short flash):

ex -s -c 'w %~ | %s/`feedback`/`comments`/g' -c 'x' <file>

Explanation:

  • w %~ saves a copy of the file with the ~ suffix. This is handy as a backup in case you mess up something.
  • | is the command separator.
  • % means "do this action on every line".
  • s/`feedback`/`comments`, as you probably already know, means "substitute text matching the regex `feedback` with `comments` on the current line".
  • g means "global", i.e. don't stop after the first substitution on the line.
  • x is a shorthand for wq, i.e. it means write the file and quit. It needs to be separate because otherwise it will hang if there's no match.

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.