LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   search file contains word (https://www.linuxquestions.org/questions/linux-newbie-8/search-file-contains-word-465940/)

prospekrisal 07-20-2006 12:05 PM

search file contains word
 
how to differ whether the file contains a word or not in shell script?
is there any simple command?

burninGpi 07-20-2006 12:24 PM

Use grep. simple sysntax:
Code:

grep <word> <file>
see man 1 grep for details.

csmarkus 07-20-2006 02:46 PM

Yes, grep is your friend. And should you be looking for which files contain the word (non case sensitive) in and below the current directory, use
Code:

grep -ri <word> .

prospekrisal 07-20-2006 07:12 PM

Im not following u guys. Why should I type the word? cud u gimme the example instead of syntax?

burninGpi 07-20-2006 07:30 PM

as an example, to see if "hello.txt" contains "bob", you would type:
Code:

grep bob hello.txt

prospekrisal 07-20-2006 09:24 PM

i've done that command in shell script

if grep for file
then
echo "The file contains the word"
else
echo "The file doesn't contain the word"
fi

output
fahrisal@linux:~> sh wordscript
for FILE in $*
The file contains the word


then I was asked by my mentor "to modify whether a parameter was passed before commencing the greppin process." wht does it mean? i dont get it.


All times are GMT -5. The time now is 06:28 AM.