Search for String within File and Return File Name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search for String within File and Return File Name
# 1  
Old 09-04-2008
Search for String within File and Return File Name

How do I search for a string within a file and return the names of the file that contain the string? I would like to search directories and sub-directories.
This User Gave Thanks to bggibson For This Post:
# 2  
Old 09-04-2008
Code:
find /top/directory -type f  -exec grep -l 'string I want to find' {} \;

These 2 Users Gave Thanks to jim mcnamara For This Post:
# 3  
Old 09-04-2008
Computer Shell script to search for strings & return file names & number of instances in each

#!/usr/bin/ksh
#
# usage: stringfinder.ksh string_to_find filesystem_to_search [eg:stringfinder.ksh test /opt/local/bin]


if [[ -z "$1" ]]; then
print -n "Which string/word/number are you searching for? : "
read STR
else
STR="$1"
fi

print "Will search for ${STR}"
if [[ -z "$2" ]]; then
print -n "Which filesystem would you like to search? : "
read FSYS
else
FSYS="$2"
fi

print "Looking for ${STR} in ${FSYS}"

print "FILES WITH ONE OR MORE INSTANCE OF ${STR}:"

for i in `find ${FSYS} -print`
do
export DIR=${i}
CT=`grep -i ${STR} ${i}|wc -l`
if [ ${CT} -ge 1 ];then print " ${DIR} has ${CT} instances";fi
done

print "Done searching for ${STR} in ${FSYS}"

unset STR
unset FSYS
unset DIR
unset CT

exit 0
# 4  
Old 09-25-2008
Data getting error

searchme.sh: line 13: print: command not found

I get the error as mentioned above.
# 5  
Old 09-25-2008
print was not a standard command in the original Bourne shell, and doesn't exist in all dialects (tho I would have thought it always existed in ksh!?). You can use echo instead.

The -n is supposed to suppress a newline after the printed message; your echo might use a different convention, such as wanting a \c at the end of string rather than the -n option. Check the manual page.

Jim's solution is more elegant, though. (And the scripter should use grep -c rather than grep | wc -l)

Last edited by era; 09-25-2008 at 06:47 AM.. Reason: Note Useless Use of wc -l
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

4. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

5. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

6. UNIX for Dummies Questions & Answers

search for string and return substring

Hi, I have a file with the following contents: I need to create a script or search command that will search for this string 'ENDC' in the file. This string is unique and only occurs in one record. Once it finds the string, I would like it to return positions 101-109 ( this is the date of... (0 Replies)
Discussion started by: Lenora2009
0 Replies

7. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies

8. Shell Programming and Scripting

Search flat file and return 3 fields

I need to be able to search a flat file (comma-separated values) for a specific value and then return the following 2 fields into variables. Here's a sample flat file: SN,Account,IPaddress W120394YF,adam,10.0.20.2 W394830PR,betty,10.0.20.3 W847582TD,charlie,10.0.20.4... (7 Replies)
Discussion started by: da2357
7 Replies

9. Shell Programming and Scripting

String search and return value from column

Dear All I had below mention file as my input file. 87980457 Jan 12 2008 2:00AM 1 60 BSC1 81164713 Jan 12 2008 3:00AM 1 60 BSC2 78084521 Jan 12 2008 4:00AM 1 60 BSC3 68385193... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

10. UNIX for Dummies Questions & Answers

Shell script to return all the ID's from file based on the distribution ID search

Hi, Please help me out on this I have to store some distribution lists(dl) in a file say mail.txt Say the file format be d1 = alok@yahoo.co.in;alvin@rediffmail.com;vijay@yahoo.com; & so on... d2 = abhinav@gmail.com;dalpit@hotmail.com;garima@yahoo.com;& so on... d3 = .... & so on Now what... (1 Reply)
Discussion started by: kumbhatalok
1 Replies
Login or Register to Ask a Question