Find a word in input file

Hi,

I had a input file containing,

abcdefghij;20100903040607;1234567891;GLOBAL;
abc123;20100903040607;12345;09;thestdf;
def456;20100903040607;67891;04;bnkim;

I need to search word GLOBAL in my file.
If it is found then
do something
else
do other thing

How can i write if loop for this criteria in .ksh file?

Use our search function right above:

Google Search Results for if grep | The UNIX and Linux Forums

Regards

I updated code with,

grep -i "GLOBAL" FILE
if [ $? -eq 0 ]
then
  echo "grep successful"
 else
  echo "unsucessful"
fi

but receiving error as ,

grep: 0652-033 Cannot open FILE.

Please help me out to resolve this error.

What does this command tell you?

ls -l FILE
-rwxr--r--   1 alf      archive          45 Sep 27 11:21 MigBIOS.CR.GLOBAL.0100924021422.CSV

So your command is:

grep -i "GLOBAL" MigBIOS.CR.GLOBAL.0100924021422.CSV

and the error message is:

grep: 0652-033 Cannot open MigBIOS.CR.GLOBAL.0100924021422.CSV

Is that correct?

YES. Thats the one.

If ls -l FILE gives:

-rwxr--r--   1 alf      archive          45 Sep 27 11:21 MigBIOS.CR.GLOBAL.0100924021422.CSV

That would make "FILE" a directory?

I am writting this code into my shell script function which takes file name as command line agrument which then i stored in FILE variable.

FILE=$1
where $1 is MigBIOS.CR.GLOBAL.0100924021422.CSV.

Hi.

So, the grep should be grep -i "GLOBAL" $FILE

Now its working fine.
Thanks a lot.
Its my silly mistake :frowning:

Could you please let me know, how can I put the if loop condition for this?

if [ `grep -i "GLOBAL" $FILE` -eq 0 ]
 then
   echo "grep successful : GLOBAL"
  elif [ `grep -i "PARTIAL" $FILE` -eq 0 ]
then
echo "grep successful : PARTIAL"
fi

Thanks in advance.

if grep -qi GLOBAL "$FILE"; then
   echo "grep successful : GLOBAL"
elif grep -qi PARTIAL "$FILE"; then
  echo "grep successful : PARTIAL"
fi

Thank you so much for your pro-active help. :slight_smile:

It's the filename itself.
:slight_smile: