![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.
|
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
find /top/directory -type f -exec grep -l 'string I want to find' {} \;
|
|
#3
|
|||
|
|||
|
#!/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
|
|||
|
|||
|
searchme.sh: line 13: print: command not found
I get the error as mentioned above. |
|
#5
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |