LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   text search (https://www.linuxquestions.org/questions/linux-desktop-74/text-search-678794/)

jnreddy 10-24-2008 07:17 AM

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

harry2006 10-24-2008 07:50 AM

Quote:

Originally Posted by jnreddy (Post 3320726)
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.

olilogu 10-24-2008 09:04 AM

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

Disillusionist 10-24-2008 04:01 PM

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)

allez 10-24-2008 10:38 PM

Quote:

Originally Posted by Disillusionist (Post 3321148)
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.

jnreddy 10-24-2008 11:52 PM

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

allez 10-25-2008 02:28 AM

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" {} \;

Disillusionist 10-25-2008 02:29 AM

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)

jnreddy 10-27-2008 05:05 AM

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

pixellany 10-27-2008 07:09 AM

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).

Disillusionist 10-27-2008 03:26 PM

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.

jnreddy 10-29-2008 04:11 AM

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

nishamathew1980 11-02-2008 05:30 AM

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


All times are GMT -5. The time now is 10:31 PM.