将 grep 的输出写入 Linux 上的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4498061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:00:58  来源:igfitidea点击:

Write output out of grep into a file on Linux?

linuxgrep

提问by Rajesh

find . -name "*.php" | xargs grep -i -n "searchstring" >output.txt

Here I am trying to write data into a file which is not happening...

在这里,我试图将数据写入一个没有发生的文件......

回答by Mikel

Looks fine to me. What happens if you remove >output.txt?

对我来说看起来不错。如果删除会发生什么>output.txt

回答by darioo

How about appending results using >>?

如何使用附加结果>>

find . -name "*.php" | xargs grep -i -n "searchstring" >> output.txt

I haven't got a Linux box with me right now, so I'll try to improvize.

我现在没有带 Linux 机器,所以我会尝试即兴创作。

the xargs grep -i -n "searchstring"bothers me a bit.

xargs grep -i -n "searchstring"让我有点困扰。

Perhaps you meant xargs -I {} grep -i "searchstring" {}, or just xargs grep -i "searchstring"?

也许你的意思xargs -I {} grep -i "searchstring" {},或者只是xargs grep -i "searchstring"

Since -nas grep's argument will give you only number lines, I doubt this is what you needed.

由于-ngrep 的论点只会给你数字行,我怀疑这是你所需要的。

This way, your final code would be

这样,您的最终代码将是

find . -name "*.php" | xargs grep -i "searchstring" >> output.txt

回答by Keith

find . -name "*.php" -exec grep -i -n "function" {} \;  >output.txt

But you won't know what file it came from. You might want:

但你不会知道它来自哪个文件。你可能想要:

find . -name "*.php" -exec grep -i -Hn "function" {} \;  >output.txt

instead.

反而。

回答by Peter van der Heijden

I guess that you have spaces in the php filenames. If you hand them to grepthrough xargsin the way that you do, the names get split into parts and grepinterprets those parts as filenames which it then cannot find.

我猜你在 php 文件名中有空格。如果你把它们交给grep通过xargs在你的方式,姓名得到分成几部分,并grep解释这些部分因为它再不能找到的文件名。

There is a solution for that. findhas a -print0option that instructs findto separate results by a NUL byte and xargshas a -0option that instructs xargsto expect a NUL byte as separator. Using those you get:

有一个解决方案。find有一个-print0选项指示find通过 NUL 字节分隔结果,并xargs有一个-0选项指示xargs期望 NUL 字节作为分隔符。使用你得到的那些:

find . -name "*.php" -print0 | xargs -0 grep -i -n "searchstring" > output.txt

回答by bear24rw

Try using line-buffered

尝试使用行缓冲

grep --line-buffered

grep --line-buffered

[edit]

[编辑]

I ran your original command on my box and it seems to work fine, so I'm not sure anymore.

我在我的盒子上运行了你的原始命令,它似乎工作正常,所以我不确定了。

回答by Andy Lester

If you're searching trees of source code, please consider using ack. To do what you're doing in ack, regardless of there being spaces in filenames, you'd do:

如果您正在搜索源代码树,请考虑使用ack。要在 ack 中执行您正在执行的操作,无论文件名中是否有空格,您都可以执行以下操作:

ack --php -i searchstring > output.txt

回答by Sam Kaz

I always use the following command. It displays the output on a console and also creates the file

我总是使用以下命令。它在控制台上显示输出并创建文件

grep -r "string to be searched" . 2>&1 | tee /your/path/to/file/filename.txt

grep -r "要搜索的字符串" 。2>&1 | 三通/你的/路径/到/文件/文件名.txt

回答by Alex

Check free disk space by

检查可用磁盘空间

$ df -Th

It could be not enough free space on your disk.

您的磁盘上可能没有足够的可用空间。