searching for content of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers searching for content of files
# 1  
Old 02-26-2007
Java searching for content of files

Hi,

This question may be quite newbish.

I've stored a few files on my Unix system and am wondering how to search for their contents (i.e. I input the keyword and get a list of files with this keyword)

I'd then like to put it on my website (php).

I thought of find and grep, but am not sure...

thx for answer in advance

Regards,
Aretai
# 2  
Old 02-26-2007
Quote:
Originally Posted by Aretai
Hi,

This question may be quite newbish.

I've stored a few files on my Unix system and am wondering how to search for their contents (i.e. I input the keyword and get a list of files with this keyword)

I'd then like to put it on my website (php).

I thought of find and grep, but am not sure...

thx for answer in advance

Regards,
Aretai
Case sensative:
Code:
grep -l "<searchstring>" <location_of_files>/*

case insensative:
Code:
grep -il "<searchstring>" <location_of_files>/*

or

case sensative:
Code:
find <top_directory_where_files_are_located> -type f -exec grep -l "<searchstring>" {} \;

case insensative:
Code:
find <top_directory_where_files_are_located> -type f -exec grep -il "<searchstring>" {} \;

# 3  
Old 02-27-2007
Re:

Thx for answer - yes I tried this command, but there are some problems...

1) The depth of the search - i.e. search on the file's parent directory is working, but when I try to search from grandgrand...parent then it's not working.

I.E I have searched for a string test
grep -l "test" and when it was about 7 folders above in hierarchy it has returned nothing, only when I was in a parent directory to a file that contained this string the name of the file was returned.

2) Some users store files from Unix (i made it possible). For example Word documents. It messes with the coding of the file and it's not possible to search for a given string.

Any solutions? Smilie

Regards,
Aretai
# 4  
Old 02-27-2007
MySQL

so u can go for the following search

grep -sxnlidU <search pattern> */*

-s for suppressing the error
-x for searching for exact match
-i for ignore casse
-r for recursive search
-U for binary interpretation
-n for line no display
-l for printing all search result
Hope this works 4 u

Last edited by preetam; 02-27-2007 at 08:07 AM..
# 5  
Old 03-01-2007
Quote:
Originally Posted by preetam
so u can go for the following search

grep -sxnlidU <search pattern> */*

-s for suppressing the error
-x for searching for exact match
-i for ignore casse
-r for recursive search
-U for binary interpretation
-n for line no display
-l for printing all search result
Hope this works 4 u
It's quite good, but the problem is I use Solaris and get:
"grep illegal option -r"
"grep -hblcnsviw pattern file"
seems U is also not allowed

(btw. The default behavior is to search only one directory, so the -r option, as provided in GNU and other implementations of grep, is the exception rather than the rule.)

Maybe Solaris forum would be bether then Smilie
# 6  
Old 03-01-2007
hi .. it works in Solaris too ..
I have checked it out.

-U and -r are supported in solaris .. check in "man grep"
# 7  
Old 03-01-2007
@ preetam

Quote:
Originally Posted by preetam
hi .. it works in Solaris too ..
I have checked it out.

-U and -r are supported in solaris .. check in "man grep"
Believe me I've checked in the manual. The problem is this is supported only by GNU version of grep (and sadly Smilie this isn't the case). I thought that there is other solution to this problem.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Searching content of a variable using awk

so i'm running a variation of the following command a few times in my script: echo "${TOTALRunning}" | awk -F"" '/'"${PROCSEARCH}"'/ {print $2}' | tr '\n' '|' unfortunately i cant paste the content of the variable TOTALRunning into this thread for security reasons. what i want to do is... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

4. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

5. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

6. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

7. Shell Programming and Scripting

Files searching

I have a list of files in directory and i should write a script if any of these files contains words given in a text file test.txt. the words can be case ignored and word should match. The output should be the name of the directory in which the file is present followed by list of file names Eg:... (1 Reply)
Discussion started by: kinny
1 Replies

8. Shell Programming and Scripting

searching content of files in the current and sub directories

Hi I was wondering why command 2 doesn't work like command 1 below. 1. find . -exec grep "test" '{}' \; -print 2. ls -R | grep "test" I am trying to search "test" from all the files in the current and sub directories. What's wrong with my command 2? Thanks in advance for your help (4 Replies)
Discussion started by: tiger99
4 Replies

9. Shell Programming and Scripting

Searching and Removing File Content

Hi, I am trying to search a character in a file and remove it from that file.... My file looks something like this: test1.txt ckj12300_00|123|var1|10.2 ckj00200_12|444|var2|11.2 ckj00200_14|4556|var3|33.5 c00200_00_000|4558|var4|33.5 ckj00200_14|4553|var5|33.5... (7 Replies)
Discussion started by: rkumar28
7 Replies

10. UNIX for Dummies Questions & Answers

Searching files..?

hi there can anyone tell me how to search and copy files under unix? im writing shell scripts with 'vi' and 'pico' something like read directoryName if then echo Copying the files copy those *.src files to sub1(another directory) using cp else ... (4 Replies)
Discussion started by: nickaren
4 Replies
Login or Register to Ask a Question