使用find和grep到Mimic findstr命令 [英] Using find and grep to Mimic findstr Command
问题描述
我最近从Windows开发环境切换到Apple开发环境.此举是一个具有挑战性的过程,但是我在终端中为基于UNIX的命令而苦苦挣扎,以补充我每天在Windows命令提示符下使用的命令.任何帮助将不胜感激,因为我试图掌握UNIX命令,所以对所提供命令的基本操作进行解释对我来说是一个巨大的收获,但是阅读man
ual就像阅读a大多数时候对我来说都是外语.
I recently switched from a Windows development environment to an Apple development environment. The move has been a challenging process, but I'm struggling with picking up UNIX based commands in terminal to makeup for the commands I used on a daily basis in Windows command prompt. Any help would greatly appreciated, explanations on a basic level of what's going on in the commands provided is a huge bonus for me as I'm trying to get a grasp on the UNIX commands, but reading the man
ual is like reading a foreign language to me most of the time.
这是我的问题:是否有一条单行命令,最好短得足以记忆一下,我可以执行该命令来模仿或产生与以下Windows CMD命令非常相似的输出:
Here's my question: Is there a single line command, preferably short enough to memorize, that I can execute to mimic or produce very similar output to the following Windows CMD command:
findstr /s /c:"this piece of text" *.code
我经常在Windows上使用此命令来生成结果集,该结果集告诉我引号之间的文本在任何子目录中与* .code模式匹配的任何文件中.这可用于检查从服务器拉回的大量文件的版本号,以查找在大型项目中声明变量的位置.输出格式如下:
I use this command on Windows often to produce a result set that shows me where the text between the quotes resides in any of the files matching the *.code pattern in any subdirectories. This can be used to check version numbers of numerous files pulled back from servers to looking for where a variable was declared in a large project. The output comes in this form:
file1.code: other text this piece of text other text
file2.code: other text this piece of text other text
file3.code: other text this piece of text other text
file4.code: other text this piece of text other text
file5.code: other text this piece of text other text
其他文本是在给定文件中与我的搜索字符串在同一行上找到的任何其他文本.我在这里搜索了所有问题,发现使用find . -name *.code
的几个人在子目录中建立了文件列表.然后,他们使用find
命令中的-exec
标志和grep
序列配对来搜索文本.我尝试过上述几种方法,但都失败了,我认为是由于转义序列或缺少字符所致.如果有一种方法可以给命令一个按原样搜索的引号之间的字符串,那就太棒了.
Where other text is any other text found on the same line as my search string in the given file. I have searched through the questions here and found several people using find . -name *.code
to build a list of files in the subdirectories. They then use the -exec
flag from the find
command paired with a grep
sequence to search text. I tried this in several of the mentioned ways and was failing, I think due to escape sequences or missed characters. It would be awesome if there was a way to just give the command a string in between quotes that it just searched for as is.
我尝试了以下操作,但没有得到任何结果……可能是语法错误?
I tried the following and wasn't getting any results... Maybe a syntax error?
find . -exec grep -H .getItemToChange().getItemAttributes()
更新 下面提供了正确的代码,John对此做了很好的解释.如果这对您有帮助,那么可以帮我答谢他!
UPDATE The correct code is provided below with a great explanation by John. If this helps you like it helped me give his answer an upvote!
我希望在一个大型项目中找到带有此函数调用的.java文件.它没有给我任何结果,也没有办法只过滤* .java.
I was hoping to find the .java file with this function call in it in a large project. It wasn't giving me any results and it also didn't have a way to filter to only *.java.
有人可以在这里帮助我吗?非常感谢您对命令的解释.
Can anyone help me out here? Explanations to your commands are GREATLY appreciated.
推荐答案
find . -name '*.code' -exec grep -H 'pattern' {} +
请确保引用'*.code'
,以便外壳程序不会扩展*
通配符.通常,我们确实希望外壳进行扩展,但是在这种情况下,我们希望将文字字符串*.code
传递给find
,以便它可以自己进行通配符扩展.
Make sure to quote '*.code'
so the shell doesn't expand the *
wildcard. Usually we do want the shell to do the expansion, but in this case we want the literal string *.code
to be passed to find
so it can do the wildcard expansion itself.
使用-exec
时,您需要将{}
放在某个位置;它是找到的文件名的占位符.在命令末尾还需要\;
或+
.这是您向find
发出信号的方式,其中-exec
的参数结尾(可能在-exec
之后执行其他操作). \;
将导致每个文件grep
运行一次,而+
在所有文件上运行单个grep
.
When you use -exec
you need to put {}
somewhere; it's the placeholder for the file names that are found. You also need either \;
or +
at the end of the command. It's how you signal to find
where the end of -exec
's arguments are (it's possible to have other actions following -exec
). \;
will cause grep
to be run once for each file while +
runs a single grep
on all of the files.
find . -name '*.code' -print0 | xargs -0 grep -H 'pattern'
执行此操作的另一种常见方法是将find
和xargs
链接在一起.我喜欢更好地使用-exec
,但是find
+ xargs
也可以正常使用.这里的想法是xargs
接受在stdin上传递的文件名,并运行带有这些文件名的命名命令.为了获得合适的文件名列表,我们将find
命令的输出通过管道传输到xargs
.
Another common way to do this is by chaining together find
and xargs
. I like using -exec
better, but find
+xargs
works just as well. The idea here is that xargs
takes file names passed in on stdin and runs the named command with those file names appended. To get a suitable list of file names passed in we pipe the output of a find
command into xargs
.
-print0
选项告诉find
打印找到的每个文件以及NUL字符(\0
).这与xargs
的-0
选项并驾齐驱.使用-print0
和-0
确保我们可以正确处理带有不寻常字符(如空格和引号)的文件名.
The -print0
option tells find
to print each file it finds along with a NUL character (\0
). This goes hand in hand with xargs
's -0
option. Using -print0
and -0
ensures that we can handle file names with unusual characters like whitespace and quotes correctly.
这篇关于使用find和grep到Mimic findstr命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!