grep на Windows 7 Неверный аргумент

659
Mike

Я использую grep из Gnuwin32 на Windows.

Выполнение:

grep -r INSERT *.sql 

Урожайность:

grep: *.sql: Invalid argument 

Есть идеи почему?

1
Вы обновили gnuwin32? Команда, кажется, работает правильно для меня. Была старая ошибка с таким поведением. monkey 8 лет назад 0

3 ответа на вопрос

2
jan.supol

Because there is no *.sql file.

Хотя это действительно так, добавление -r приводит к той же ошибке. Mike 8 лет назад 0
@Mike bash расширяет `* .sql` для всех файлов / каталогов, соответствующих этому шаблону. Так как ничего не соответствует, вы получите ошибку. `-r` не имеет к этому никакого отношения fedorqui 8 лет назад 0
Извините, я не следую. В папке, где я выполнил команду, нет файлов с расширением .sql. Есть подпапки, и у них действительно есть файлы с расширением .sql. Разве `-r` не должен помочь тогда? Mike 8 лет назад 0
[TECHIE007] (http://superuser.com/users/23133/%c6%ac%e1%b4%87c%ca%9c%ce%b9%e1%b4%87007) имеет хороший ответ на этот вопрос. Совершенно не связанный с вашим вопросом, возможно, для развлечения или для сравнения, я хотел бы просто добавить решение, не относящееся к Gnuwin32, которое, вероятно, делает то же самое: `@for / f% i in ('dir / s / b *. sql ') do @ (введите% i | find "INSERT"> nul &&, если errorlevel 0 echo% i) `. jan.supol 8 лет назад 0
2
Ƭᴇcʜιᴇ007

According to the Grep manual:

-r with grep is recursive directory searching, so to use it you specify the starting directory, not a file mask.

e.g.:

grep -r INSERT . would look at all files for INSERT, starting at the current directory (.) and recursively working its way through the sub-folders.

To specify recursive folder checking and specify a file wildcard to limit searches, you can use the --include option:

grep -r --include "*.sql" INSERT .

Similar question/info over on StackOverflow: How do I grep recursively?

Я думаю, что мне нужно обновить мой grep. У меня нет опции --include! Но это имеет большой смысл. Mike 8 лет назад 0
1
fedorqui

grep is a great tool with some interesting parameters. However, as its name says (globally search a regular expression and print) it is meant for matching things. If what you want is to find files, use find.

In this case, it looks like you want to look for the text INSERT within files that are in this tree.

For this, you need to do something like:

find -name "*.sql" grep -h INSERT {} \; 

find -name "*.sql" will find all these files and then grep -h will print those having the text INSERT in them.


Why wasn't your approach working?

Because by saying grep -r ... *.sql, bash tries to expand that *.sql before performing the command. But nothing matches *.sql in your directory, so it cannot work.

You can use the --include parameter with a regexp, but -in my experience- it is quite fragile.