views:

62

answers:

3

Hello, I have a little Vim script which does a multi-line search and replace:

vim -c 's/^ *<hi a=\"26\">\nHello/<td height=\"26\">\r<\/table>\r<bla \/>' \
    -c 'w!' -c 'q' test.html

That works. However, when I put that in a find -exec to do this recursively in the directory:

find . -iname 'test.html' -exec \
    vim -c 's/^ *<hi a=\"26\">\nHello/<td height=\"26\">\r<\/table>\r<bla \/>' \
    -c 'w!' -c 'q' \
{} \;

test.html remains unchanged, and Vim gives me this error:

Pattern not found:
  ^ *<hi a=\"26\">\nHello
in ./test.html

This is really strange because that is the correct regex, and I can search for it manually in Vim with success.

Can you see any obvious errors with my find syntax?

A: 

You have to escape the braces in the exec phrase (assuming you didn't, maybe the forum posting ate the back slashes). It should end \{\} \;

verisimilidude
Thanks, I escaped the curly braces. My original problem persists, though :(
nnyby
+3  A: 

I figured it out! The search pattern needed a % character: vim -c '%s/switch/to/' is the correct syntax.

nnyby
+2  A: 

When I do stuff like this, I tend to do it within vim.

:vimgrep /^ *<hi a=\"26\">\nHello/ **/*

followed by the creation and execution of a recursive macro:

qbq
qa (actually you don't press enter after this)
:silent! !p4 edit %  " check out the file
:e  " refresh the r/w status
:%s/^ *<hi a=\"26\">\nHello/<td height=\"26\">\r<\/table>\r<bla \/>
:w  " no need to force write if it's r/w
:cnf
q
qb@a@bq
@b
dash-tom-bang