LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 10-24-2008, 07:17 AM   #1
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Rep: Reputation: 15
text search


hi all

i want a search a text match, means a word it may be somewhere in some file.

for example assume a user name is test1

if i type: find / -name test1
it should search all files having this line (test1) match
i think the text (test1) should be somewhere in /etc/passwd

Thanks in Advance
jnreddy
 
Old 10-24-2008, 07:50 AM   #2
harry2006
Member
 
Registered: Aug 2006
Location: /hawaii/honolulu/downtown
Distribution: Fedora 10[Cambridge] and Ubuntu 9.04[Jaunty]
Posts: 201

Rep: Reputation: 30
Quote:
Originally Posted by jnreddy View Post
hi all

i want a search a text match, means a word it may be somewhere in some file.

for example assume a user name is test1

if i type: find / -name test1
it should search all files having this line (test1) match
i think the text (test1) should be somewhere in /etc/passwd

Thanks in Advance
jnreddy
You are not clear about what exctly you want. as far as I know there are basically two things available by default for searching e.g
1. if you are searching a file with a given name, then you can use the "find" command available in linux
2. if you are looking for a file that contains some specific words i.e you want to look inside the file then make use of "grep/rgrep/fgrep etc" which does the job.
If you are looking for someting else do let us know, may be you want to write some scripts/wrappers using the above mentioned commands. do let us know what exactly you want then we'll be in a better positon to help you out. thank you.
 
Old 10-24-2008, 09:04 AM   #3
olilogu
LQ Newbie
 
Registered: Oct 2008
Location: EST USA
Posts: 3

Rep: Reputation: 0
Smile find and grep

If you need to looking for a file with word "test1" you can use:
find / -name "*test1*"

Other wise if you need to look for a certain text on specify file you can use:
grep -i "test1" /etc/passwd

It will look for the word test1 on file /etc/passwd

Now if you prefer combine both, Ex. search on directory /home/test all file txt that have "test1" word in in, do:
$ grep test1 `find /home/test/*.txt -name "*.txt"`

I hope this will help you
 
Old 10-24-2008, 04:01 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Combine find and grep to find files that contain a specific word or phrase:

Code:
find /home/dis -type f|grep -l "Test data"
will produce a list of all files that contain the string Test data

However, it should be noted that, if this string is not on the same line, then the file would not be found (and of course you need permissions to read the file for the grep statement to work)
 
Old 10-24-2008, 10:38 PM   #5
allez
Member
 
Registered: Jul 2008
Location: Russia/Siberia/Krasnoyarsk
Distribution: SuSE, CentOS, FreeBSD
Posts: 77

Rep: Reputation: 21
Quote:
Originally Posted by Disillusionist View Post
Combine find and grep to find files that contain a specific word or phrase:

Code:
find /home/dis -type f|grep -l "Test data"
will produce a list of all files that contain the string Test data
A small note - there's no need to use a pipe: find /home/dis -type f -exec grep -l "Test data" {} \; does the same thing.
 
Old 10-24-2008, 11:52 PM   #6
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Original Poster
Rep: Reputation: 15
Still probs

Thanks to allez,Disillutionist,olilogu,harry2006

I want to find files that contain a specific word or phrase

i just created a file with vi editor

vi test1, then i inserted a word named monkey in that,so test1 file contains a string called monkey. i tried below command to search that string monkey.

find / -type f|grep -1 "monkey"

it doesn't find the string

Thanks In Advance
jnreddy
 
Old 10-25-2008, 02:28 AM   #7
allez
Member
 
Registered: Jul 2008
Location: Russia/Siberia/Krasnoyarsk
Distribution: SuSE, CentOS, FreeBSD
Posts: 77

Rep: Reputation: 21
Quote:
Originally Posted by jnreddy
find / -type f|grep -1 "monkey"
You should be more careful on reading others' posts. Or just use "copy-paste" method. And, of course, read man grep about -l option ("ell", not "one").
Try this command:
Code:
find / -type f -exec grep -l "monkey" {} \;

Last edited by allez; 10-25-2008 at 02:31 AM.
 
Old 10-25-2008, 02:29 AM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
That should be a lower case L character in the grep statement, not a number.

Also I would suggest not running find from / as you are likely to get a large amount of permission denied messages (unless you're running as root)
 
Old 10-27-2008, 05:05 AM   #9
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Original Poster
Rep: Reputation: 15
sorry,still prob

Thnkq allez disillutionist

am extremely sorry for improper observation did not notice that 1(ell) or l.(one).

now i tried the command but unfortunately am facing this err
and also i tried with uppercase L(ell) i got the same error.



[root@server1 ~]# find / -type f -exec grep -l "monkey" {} \;
grep: /var/named/chroot/proc/acpi/event: Device or resource busy
grep: /var/named/chroot/proc/sys/net/ipv6/route/flush: Operation not permitted
grep: /var/named/chroot/proc/sys/net/ipv4/route/flush: Operation not permitted
grep: /var/named/chroot/proc/sysrq-trigger: Invalid argume

cursor is continously blinking for ever?

Thanks In Advance
jnreddy

Last edited by jnreddy; 10-27-2008 at 05:06 AM. Reason: spelling mistake
 
Old 10-27-2008, 07:09 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Assuming that you have the syntax right, is it possible that there is something about those files that makes them do strange thing with GREP? (Try it and see.)

Repeating earlier advice: You do not normally want to run find starting at the root directory (/). Use something like /home or /usr (or wherever the files are that you want to test).
 
Old 10-27-2008, 03:26 PM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
[root@server1 ~]# find / -type f -exec grep -l "monkey" {} \;
grep: /var/named/chroot/proc/acpi/event: Device or resource busy
grep: /var/named/chroot/proc/sys/net/ipv6/route/flush: Operation not permitted
grep: /var/named/chroot/proc/sys/net/ipv4/route/flush: Operation not permitted
grep: /var/named/chroot/proc/sysrq-trigger: Invalid argume

cursor is continously blinking for ever?
I am not surprised that these files would cause problems.

The proc folder structure enables interaction with hardware and kernel functions and should not be treated as you would normal files.
 
Old 10-29-2008, 04:11 AM   #12
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 171

Original Poster
Rep: Reputation: 15
Thumbs up problem resolved

Thanks to all of them, who spent their valuable time to give support to resolve this issue.

as you said i tried with /home.
find /home -type f -exec grep -l "monkey" {} \;


Thanks in advance
jnreddy

Last edited by jnreddy; 10-29-2008 at 04:13 AM. Reason: spelling mistake
 
Old 11-02-2008, 05:30 AM   #13
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
Use the command grep -ri "keyword_you_are_looking_for" <locations_you_want_to_search_for_this_keyword>



This should do the trick.


Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:51 AM.
 
  


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 search for text strings? armandino LQ Suggestions & Feedback 2 06-24-2007 07:51 PM
How to search within text file 666 Linux - General 2 05-09-2007 02:26 PM
Search text in files remotely anjanesh Red Hat 2 08-15-2005 09:03 AM
Search all text files? nextekcarl Linux - General 4 10-20-2004 12:18 PM
what tool can search for documents containing certain text? jacksonscottsly Linux - Software 3 07-19-2004 01:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 04: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
Open Source Consulting | Domain Registration