crackerplace
crackerplace

Reputation: 5475

Shell Script : If a string is present in a file

I am a newbie to shell scriptng and I want to check if 3 strings("hello","who","when " etc) are present in a file.
I find many ways when I google out awk,cat ,grep etc ,What can be the best way and how Can I do it.
I just need to know if the strings are present or not .

Upvotes: 1

Views: 7133

Answers (1)

Roland Illig
Roland Illig

Reputation: 41625

Your question is a little incomplete:

  • do you want to find strings or words? So when the word Othello appears, does that count as hello?
  • in your question there is whitespace behind the when. Is that intentional?
  • do you want to know whether all three words are in the file, or is one of the words enough?

The general solution is to use grep or egrep to search for text in a file. The exact command line depends on the answers to the above questions.

  • to search for words (Othello doesn't count as hello) you need to pass the -w option to grep.
  • I'm assuming thhat the whitespace was a mistake.

When you need all the words, you can do egrep -wo 'hello|who|when' | sort -u. The egrep command finds all instances of the given words, and prints them out one per line. At that point, you will have many duplicates. Therefore the sort -u command sorts them and only keeps the unique lines (that's what the -u means). In a complete program, I would do it as follows:

filename="story.txt"
words=$(egrep -wo 'hello|who|when' "$filename" | sort -u)
n=$(echo "$words" | wc -l)
if [ $n = 3 ]; then
  echo "found all words in the file"
else
  echo "didn't find all words, only \""$words"\"."
fi

There's a lot more that I could tell you about this little piece of code, and why I wrote it exactly like that, but for a beginner, it's already enough to understand.

But just in case that you need a simple solution and the file is small anyway, so performance is not critical, you can do this:

filename="story.txt"
if egrep -wl 'hello' "$filename" 1>/dev/null; then
  if egrep -wl 'when' "$filename" 1>/dev/null; then
    if egrep -wl 'who' "$filename" 1>/dev/null; then
      echo "found all three words"
    fi
  fi
fi

[Update:]

This second code snippet also checks whether the given file contains all three words. Each of the if clauses checks for one of the words. The option -l (lowercase ell) to egrep makes it potentially faster, but you probably don't need that option at all.

Normally egrep prints all lines that match the given expressions (your three words in this case). Since we don't need that output, we redirect it using the arrow operator > to a special file called /dev/null. Whatever you write into that file is discarded.

The if statement takes another command as its argument, and if that command returns successfully, the then branch is taken. The nice thing about the egrep command is that it returns successfully iff the given search expression is contained in the file, so these two things perfectly fit together.

For further reading you should try the reference documentation from the Open Group website: http://www.google.com/search?q=opengroup+grep

Upvotes: 1

Related Questions