Shell Script Problem (NEWBIE)

July 15, 2011 at 12:49:52
Specs: Win 7
Hi guys I have this problem... I am supposed to Check if the word i had input, exist in the txt.file
And i am having a problem with my codes...

cat File.txt | while read TEXT
if [ grep $input $TEXT -eq 0]
echo "Found"
else
echo "Not found!"
fi


See More: Shell Script Problem (NEWBIE)

Report •

#1
July 15, 2011 at 17:28:19
First, if you want to know if $input exists in the file, why are you reading each line of the file? There is no reason to:

#!/bin/bash

input="myword"
if grep "$input" File.txt > /dev/null
then
  echo "Found"
else
   echo "Not Found"
fi

Second, keep in mind that the grep command as you have written it is not searching for a word - but a string meaning in the example above, not only will recognize the word " myword", but "xmyword".


Report •

#2
July 16, 2011 at 02:21:38
Hey thanks!

But what if i got 2 different $input to check for in my txt.file?

do i...?

if grep $input1 && $input2 FIle.txt > /dev/null


Report •

#3
July 16, 2011 at 15:33:23
The extended grep command egrep allows searching for alternate inputs using the "or" construct which is the pipe symbol:

if egrep "$input1|$input2" File.txt > /dev/null
then
  echo "Found"
else
   echo "Not Found"
fi


Report •

Related Solutions

#4
July 17, 2011 at 10:34:00
Thanks alot !!!!!! =)

Report •

Ask Question