Grep two word and one word with if


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep two word and one word with if
# 1  
Old 03-04-2013
Grep two word and one word with if

so far I have the following , it work,
Code:
if grep -w "Hi"\|"Hello" /home/my.log 2>&1 > /dev/null 
then
    EMAIL_ADDR="aaa@gmail.com"
    echo "Please view the error messages on attached file " | mutt -a ~/error_report.txt -s "hi `date +"%d-%m-%Y"`" "$EMAIL_ADDR"
  fi

But SmilieI want to add if only "Hi" but not "Hello" in the same file
email out to other place withe different message

Last edited by Franklin52; 03-04-2013 at 05:00 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 03-04-2013
Please use code tags for data and code as required by forum rules!

That \| is an OR operator in regexes, so your grep will be happy if it matches either. In order to exclude "Hello", you can try
Code:
$ grep -q "Hello" file || { grep -q "Hi" file && echo "fine" ; }
                       ^--- if NOT found      ^--- if found

# 3  
Old 03-04-2013
Try:
Code:
if grep -wq "Hi" file && ! grep -wq "Hello" file
then
  echo "Hi, but not hello"
fi

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-04-2013
Thanks for all the replies!
but I need the
Code:
if grep -wq "Hi" file && ! grep -wq "Hello" file
one kid of email
if grep -wq "Hi" file &&  grep -wq "Hello" file
 other kind of mail

on meantime I try , always get error on the else on the first fiSmilie
Code:
if grep -w "Hi"\|"Hello" /home/my.log 2>&1 > /dev/null
then
 one kid of email
else 
   if if grep -wq "Hi" /home/my.log 2>&1 > /dev/null
  then
   other kind of mail
      fi
fi

---------- Post updated at 09:40 AM ---------- Previous update was at 09:34 AM ----------

I just wonder what is the correct syntax for
grep with the nest if else Smilie

---------- Post updated at 09:50 AM ---------- Previous update was at 09:40 AM ----------

what is different for the folowing three kinds of
HTML Code:
1) if grep xxx ; then
 2) if grep xxx 
     then
3)  if [ grep xxx  ]  ; then
for the 2) with nest if else I often get syntax error
for the 1) always get too many arguments
!!
whySmilieSmilie

Last edited by Scrutinizer; 03-04-2013 at 11:03 AM.. Reason: icode tags changed to code tags
# 5  
Old 03-04-2013
Try:
Code:
if condition 1; then
  action 1
elif condition 2; then
  action 2
fi

1) and 2) are the same. Option 3 cannot be used like that...
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-04-2013
RedHat

try this
Code:
egrep -wq "Hi|Hello" /home/my.log
if [ "$?" -eq 0]
then
           one kind of email
else
     grep -wq "Hi" /home/my.log
     if [ "$?" -eq 0] 
    then    
        other kind of mail       
    fi
fi

# 7  
Old 03-05-2013
Seems like you don't like the short approach - anyhow...
If you really want to distinguish between all possibilities of occurrences of your keywords, try
Code:
grep -qw hello file; TMP=$?; grep -qw hi file; RES=$((2-2*$?+1-TMP))

case $RES in
        0) echo mail: no occurrence;;
        1) echo mail: hello occurrence;;
        2) echo mail: hi occurrence;;
        3) echo mail: both occurrences;;
    esac


Last edited by RudiC; 03-05-2013 at 02:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for a word or word with underscore

I have a file "test" with following contents: cat test abc abcd_efg abc_abc I want to only grep for abc or abc_ without getting other results, how do I achieve this? If I use grep -w abc test option I get only abc and not abc_. If I use egrep "abc|abc_" test its still printing... (3 Replies)
Discussion started by: ctrld
3 Replies

2. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

3. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

4. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

5. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

6. Shell Programming and Scripting

How to grep a word and next column to that word?

Hi, I have input file as below. Can you help me? inac_4y;0;2;Balance;200;1;1; 0;2;Balance;100;1; 0;inac_nq;0;1;Balance;100;1 desired output Balance;200 Balance;100 Balance;100 -Suresh Please use and tags when posting code, data or logs etc. to preserve formatting... (5 Replies)
Discussion started by: suresh3566
5 Replies

7. Shell Programming and Scripting

Grep for a particular word and get only the word

HI, Let us a consider i have a file as following. abcde (flag5 / 234 ) Mod 45 efgh afghd (flag3/ 343) MOD 34 ghdd tryd (t_flag6/ 567 ) MOD 43 uifudiu Is there a way where I need only the flag and the calculation done on it. The output should be : (flag5 / 234 ) Mod 45 ... (8 Replies)
Discussion started by: ashwin3086
8 Replies

8. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

10. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies
Login or Register to Ask a Question