LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Output to a text file, the results of compound command (https://www.linuxquestions.org/questions/linux-general-1/output-to-a-text-file-the-results-of-compound-command-822586/)

Herbivore 07-28-2010 04:37 AM

Output to a text file, the results of compound command
 
How to output to a text file the compound command:
Code:

find -type f -print0 | xargs -0 grep -l "desired text"
I have not been able to find the answer.
Code:

>
works for a simple command:
Code:

ls -al /etc > etc.txt
or, to add to the existing file etc.txt:
Code:

ls -al /etc/inittab >> etc.txt
but not for the compound find command above.

Thanks in advance!

b0uncer 07-28-2010 04:59 AM

What goes wrong with this?
Code:

find -type f -print0 | xargs -0 grep -l "desired text" > output.txt
I may be doing a mistake here myself, not getting the same result you do (if you have problems) but I *think* I got the output of that (prior to >) saved to output.txt exactly as it was supposed to.

GrapefruiTgirl 07-28-2010 05:03 AM

Your compound command works fine for me, using shell redirection:
Code:

root@reactor: find ./ -type f -print0 | xargs -0 grep -l "Kalli"
./Kalli/Kalliste.log
./Downloads/fire/firefox/dictionaries/en-US.dic
root@reactor: find ./ -type f -print0 | xargs -0 grep -l "Kalli" >> GREPLOG
root@reactor: cat GREPLOG
./Kalli/Kalliste.log
./Downloads/fire/firefox/dictionaries/en-US.dic
root@reactor:

Maybe you are simply not finding any matches?

b0uncer 07-28-2010 05:17 AM

I'll post a short testcase here, because GrapefruiTgirl seems to agree with me, that there's nothing wrong. This is a tad shorter than what the OP posted, but nevertheless..

First, let's set up some files. Two that contain "a" and one that does not. When looking for files containing "a", we ought to find the two, of course, and not the third one.

Code:

cd /tmp
echo "a" > a
echo "b" > b
echo "a b" > ab


Then find and xargs combination
Code:

find -type f |xargs grep -l "a"
which produces output

Code:

./a
./ab

Same again, with redirection to out.txt
Code:

find -type f |xargs grep -l "a" > out.txt
and looking inside of the out.txt file,
Code:

cat out.txt
produces
Code:

./a
./ab

As a sidenote, you don't necessarily need to use xargs:
Code:

find -type f -exec grep -l "a" '{}' \;
produces
Code:

./a
./ab

which is the very same output.

Cleaning up the temporary files:
Code:

rm a b ab
So, to conclude, there ought to be nothing wrong here, except for the results find produces. Before passing the results on, you should check that you find what you're looking for.

ghostdog74 07-28-2010 05:17 AM

Code:

grep -Rl "desired text" *

Herbivore 07-28-2010 10:38 AM

Well, they don't call us 'Newbies' for no reason! I absolutely have no clue why this did not work for me when I posted the question, but it works now. Thank you all for the answers and for the elaborations which went beyond the question.


All times are GMT -5. The time now is 12:55 PM.