Computing.Net > Forums > Unix > if word exit in the file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

if word exit in the file

Reply to Message Icon

Name: lndsey
Date: February 25, 2008 at 09:15:11 Pacific
OS: unix/linux
CPU/Ram: none
Product: none
Comment:

I am trying to wite shell script and running into little trouble. I need to find a particular word in the file, then copy that file into a different folder. I can find the word in the file, but having problem on how to return status if find it or not. Maybe an if/else statement to see if word exit in the file. Any suggestion or recommendation is appricated. Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 25, 2008 at 12:01:06 Pacific
Reply:

There are a number of ways to search for a pattern in a file. grep searches for a pattern. You can use the exit status, $?:

Assuming you are searching for pattern 'word' in 'myfile':

#!/bin/ksh

grep word myfile > /dev/null
if [[ $? -eq 0 ]]
then
echo "found the word"
else
echo "Did NOT find the word"
fi

You can also look at a count of the pattern in a file:

#!/bin/ksh

count=$(grep -c word myfile)
if [[ $count -gt 0 ]]
then
echo "found the word"
else
echo "Did NOT find the word"
fi



0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: if word exit in the file

Extract the max no. in the File Ext www.computing.net/answers/unix/extract-the-max-no-in-the-file-ext/4485.html

Find duplicate words in a file www.computing.net/answers/unix/find-duplicate-words-in-a-file/7999.html

Word count in unix www.computing.net/answers/unix/word-count-in-unix/7143.html