LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2006, 04:28 PM   #1
snoozing
Member
 
Registered: Jan 2006
Posts: 38

Rep: Reputation: 15
Find a file with text "abc"


I am looking for a find function which
will seek a file containing a text string.

Don't say grep because it doesn't work.

I'm looking for something I can do on the command line.
 
Old 01-31-2006, 04:34 PM   #2
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 124Reputation: 124
Tell us what you're doing -- grep _should_ do it. Maybe you forgot to include the insensitive search or the recursive option? Please post the command you entered at the command line.
 
Old 01-31-2006, 04:42 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
grep grep grep grep grep grep
Of course grep works---but it has to be used in conjuction with other things.
File is myfile
target string is xyz

cat myfile | grep xyz prints out each line of the file that contains "xyz" Returns exit status of 0 if the string is found at least once, and 1 if there were no matches.
Follow with echo $? to display the exit status.

Put all this together and you can write a script that tells you what files contain the string--and the number of occurences, if you desire.

To really get into this kind of thing, I suggest "Bash guide for Beginners", by Machtelet Garrels......tldp.org

This is inelegant and only one of many ways to do the same thing.
 
Old 01-31-2006, 05:00 PM   #4
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 124Reputation: 124
To add two simple searches for files containing xyz in the current directory and containing folders, both output just the filename, not the line containing the search pattern itself:

grep -R -l xyz *
find . -type f -exec grep -l xyz {} \; -- searches also in hidden directories and files
 
Old 02-01-2006, 03:50 AM   #5
snoozing
Member
 
Registered: Jan 2006
Posts: 38

Original Poster
Rep: Reputation: 15
Works now,
the cat filename | grep xyz
and the grep -R -l xyz *

I don't understand what went wrong before--stress.

find . -type f -exec grep -l xyz {} \; => error:find paths must precede expression
a pipe missing?
 
Old 02-01-2006, 04:59 AM   #6
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
Quote:
Originally Posted by pixellany
cat myfile | grep xyz
Is there any benefit/difference to grep reading from a pipe rather than from an input file, as in 'grep xyz myfile'?
 
Old 02-01-2006, 05:44 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by snoozing
find . -type f -exec grep -l xyz {} \; => error:find paths must precede expression
a pipe missing?
find . -type f -exec grep -l xyz {} \; works on my boxes (slack and rh). However, find . -name * -exec grep -l xyz {} \; gives the error message as well. Solution is to put the * between double quotes like find . -name "*" -exec grep -l xyz {} \; or to put a backslash before the * (see man find)
 
Old 02-01-2006, 08:07 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by Dtsazza
Is there any benefit/difference to grep reading from a pipe rather than from an input file, as in 'grep xyz myfile'?
Interesting question....does a pipe actually slow things down or is it just a syntactic variation that produces that same machine code?
One advantage of the way I wrote it could be readability for the beginner.
 
Old 02-01-2006, 11:17 PM   #9
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by pixellany
One advantage of the way I wrote it could be readability for the beginner.
Your view A real beginner does not know about pipes , so the plain 'grep' is better in my view.

I ran a small test on a 200MByte text file. Don't know if it's really a reliable one:
Code:
time grep mytxt myfile
real 17.1
user 16.9
sys  0.16

time cat myfile | grep mytxt
real 17.0
user 16.7
sys  0.32
The commands were run a couple of times in random sequece (mixing both commands to 'eliminate' possible cache influences).

It surprised me that the 'cat' version actually was faster everytime it was executed. If other processes would have influenced the results, that would be equally true for both versions of the command.

PS: 'mytxt' only occured once as the first word on the last line
PS2: results are rounded

Last edited by Wim Sturkenboom; 02-01-2006 at 11:18 PM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 08:54 AM
How can I catch some "KEY WORDS" from a text file without opening it? yuzuohong Linux - General 1 06-14-2002 09:27 PM


All times are GMT -5. The time now is 11:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration