开发者

Write output out of grep into a file on Linux?

find . -n开发者_运维知识库ame "*.php" | xargs grep -i -n "searchstring" >output.txt

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


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.

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

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

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

This way, your final code would be

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


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.


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

There is a solution for that. find has a -print0 option that instructs find to separate results by a NUL byte and xargs has a -0 option that instructs xargs to expect a NUL byte as separator. Using those you get:

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


Try using 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.


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


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 --php -i searchstring > output.txt


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


Check free disk space by

$ df -Th

It could be not enough free space on your disk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜