954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to check for existence of particular word in file

Trying to code a shell script for a program testing. i need to check for the existence of particular word in outfile and then proceed further, which i am not able to do. I have wrote the following code. Please tell the corrections.. The control does not pass to the pass condition...

g++ findword.cpp
./a.out < file2.txt > outfile
if read outfile = "enter the word to be matched word found"]
then
echo pass
else
echo fail
fi

Shruti4444
Newbie Poster
7 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

you need grep -q (Chant man grep then check the -q option, then experiment a little bit until you understand how it works.) Hint: A zero status is the way Unix utilities signal 'it worked'; so you can use it in a logic test.

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

Trying to code a shell script for a program testing. i need to check for the existence of particular word in outfile and then proceed further, which i am not able to do. I have wrote the following code. Please tell the corrections.. The control does not pass to the pass condition...

g++ findword.cpp
./a.out < file2.txt > outfile
if read outfile = "enter the word to be matched word found"]
then
echo pass
else
echo fail
fi

[indent]

printf "%s " "enter word to be matched: "
IFS= read -r word
if grep -q "$word" outfile
then
  echo pass
else
  echo fail
fi

[/indent]

cfajohnson
Junior Poster
196 posts since Dec 2008
Reputation Points: 25
Solved Threads: 23
 

cat filename | grep word | echo "$?"

if the result of above oneliner is 0 it means the word is there in the file . if it's non zero
it means word is not there.(echo "$?" tells us whether the previous command was successful
or not)


let say your file is abc.txt and you want to search the word "john" in it.
cat abc.txt | grep john | echo "$?"

smrati.katiyar
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

cat filename | grep word | echo "$?"

if the result of above oneliner is 0 it means the word is there in the file . if it's non zero it means word is not there.(echo "$?" tells us whether the previous command was successful or not)

let say your file is abc.txt and you want to search the word "john" in it. cat abc.txt | grep john | echo "$?"

# why this:
cat filename | grep word
# instead of:
grep word filename
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

@smrati-the code you wrote is just too complex when we can write simpler codes ....using pipe in scripting is required if their are many operations to be performed...
just by writing filename we can work with grep command
not required

cat filename | grep word

just write this way....

grep word filename
IIM
Junior Poster
165 posts since Jun 2011
Reputation Points: 46
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You