The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can search a String in one text file and replace the whole line in another file kkraja UNIX for Dummies Questions & Answers 6 08-06-2008 04:23 AM
String search and return value from column jaydeep_sadaria Shell Programming and Scripting 3 01-12-2008 01:31 AM
how to search string and number in one file and check in the other file knshree Shell Programming and Scripting 9 08-24-2007 01:29 AM
Shell script to return all the ID's from file based on the distribution ID search kumbhatalok UNIX for Dummies Questions & Answers 1 10-06-2006 09:53 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 05:53 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-04-2008
Registered User
 

Join Date: Aug 2008
Location: Roanoke, VA
Posts: 4
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.
Reply With Quote
Forum Sponsor
  #2  
Old 09-04-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
Code:
find /top/directory -type f  -exec grep -l 'string I want to find' {} \;
Reply With Quote
  #3  
Old 09-04-2008
Registered User
 

Join Date: Sep 2008
Posts: 1
Talking 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
Reply With Quote
  #4  
Old 09-25-2008
Registered User
 

Join Date: Aug 2008
Location: Mumbai
Posts: 29
Unhappy getting error

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

I get the error as mentioned above.
Reply With Quote
  #5  
Old 09-25-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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 02:47 AM. Reason: Note Useless Use of wc -l
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0