LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-28-2010, 05:37 AM   #1
Herbivore
LQ Newbie
 
Registered: Mar 2008
Posts: 17

Rep: Reputation: 0
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!
 
Old 07-28-2010, 05:59 AM   #2
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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.

Last edited by b0uncer; 07-28-2010 at 06:01 AM.
 
Old 07-28-2010, 06:03 AM   #3
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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?
 
Old 07-28-2010, 06:17 AM   #4
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
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.
 
Old 07-28-2010, 06:17 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
grep -Rl "desired text" *
 
Old 07-28-2010, 11:38 AM   #6
Herbivore
LQ Newbie
 
Registered: Mar 2008
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect the SCP command output to text file. gokuls Linux - General 10 02-22-2012 03:12 AM
Are results sent to an output file only at the end of a command sequence? Zebe Linux - Newbie 2 03-19-2009 07:34 PM
Shell Script format question - output command results in quotes Barefootpanda Linux - General 5 10-14-2008 12:40 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
command output to text file? meniscus Linux - Newbie 4 10-05-2006 06:29 AM


All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration