2

I'm trying to use find in Windows 7 with GNU sed to recursively replace a line of text in multiple files, across multiple directories. I looked at this question but the PowerShell solution seems to work with only one file, and I want to work with all files with a certain extension, recursively from the current directory. I tried this command:

find "*.mako" -exec sed -i "s:<%inherit file="layout.mako"/>:<%inherit file="../layout.mako"/>:"

But that gives me a bunch of crap and doesn't change any files:

---------- EDIT.MAKO

File not found - -EXEC
File not found - SED
File not found - -I
File not found - LAYOUT.MAKO/>:<%INHERIT FILE=../LAYOUT.MAKO/>:

How can I do this? It seems like I should have all the tools installed that I need, without having to install Cygwin or UnixUtils or anything else.

Edit: okay, working with GNU find, I still can't get anywhere, because I can't get the find part to work:

> gfind -iname "*.mako" .
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
> gfind . -iname "*.mako"
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
> gfind -iname "*.mako" .
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression

I was originally not using GNU find in Windows 7 because of this question.

Edit:

I tried the following, but sed doesn't see any input files this way:

> ls -r | grep mako | sed -i 's/file="layout.mako"/file="..\/layout.mako"/'
sed.exe: no input files
Community
  • 1
  • 1
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222

6 Answers6

3

FIND from windows is being found instead of find from gnu.

So, rename your find.exe (from gnu) to gfind.exe (for example) and then call gfind instead of find when you wish to run it.

[edit]
gfind . -name "*.mako" (not gfind -iname "*.make" .)
[/edit]

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
1

In Powershell, note the escape sequence using backticks

find . -type f -exec grep hello  `{`} `;

Its much easier to use xargs

find . | xargs grep hello
Andrew Murphy
  • 1,336
  • 1
  • 11
  • 10
1

You're executing the regular windows 'find' command, which has completely different command line arguments than gnu find. MS find has no capability of executing a program for each match, it simply searches.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Addition to Marc B/KevinDTimm answers: your find syntax is wrong.

It is not:

find "*.mako"

but:

find -name "*.mako"

Also, if there are directories that matches "*.mako", they would be sent to sed. To avoid that:

find -name "*.mako" -type f

Finally, I think that you are missing a '\;' at the end or your find command.

jfg956
  • 16,077
  • 4
  • 26
  • 34
0

I tried the following, but sed doesn't see any input files this way:

ls -r | grep mako | sed -i 's/file="layout.mako"/file="../layout.mako"/' sed.exe: no input files

With this you are now running into PowerShell's "ls" alias. Either call "ls.exe" or go all PowerShell like this:

ls -r | select-string mako -list | select -exp path | sed -i 's/file="layout.mako"/file="..\/layout.mako"/'

Edit:

Workaround if stdin handling doesn't seem to be working.

ls -r | select-string mako -list | select -exp path | % {sed -i 's/file="layout.mako"/file="..\/layout.mako"/' $_}
Community
  • 1
  • 1
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • `PS > ls -r | select-string mako -list | sele ct -exp path | sed -i 's/file="layout.mako"/file="..\/layout.mako"/' sed.exe: no input files`, whereas doing all the command but the `sed` part does get a correct list of files. – Sarah Vessels Jul 22 '11 at 14:16
  • I think `sed` is doing something unusual here. The file paths should be going into `sed`'s stdin. – JasonMArcher Jul 22 '11 at 17:38
0

Per your

Edit:

I tried the following, but sed doesn't see any input files this way:

ls -r | grep mako | sed -i 's/file="layout.mako"/file="../layout.mako"/' sed.exe: no input files

you need to use xargs to assemble the list of files passed to sed, i.e.

ls -r | grep mako | xargs sed -i 's\:file="layout.mako":file="../layout.mako":'

Note that for most versions of sed, you can use an alternate character to identify the substitute match/replace strings (usually '/'). Some seds require escaping that alternate char, which I have done in this example.

I hope this helps.

shellter
  • 36,525
  • 7
  • 83
  • 90