if clause with grep and word count


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if clause with grep and word count
# 1  
Old 03-10-2009
if clause with grep and word count

I've got following script that I thought would only email me when the if clause finds the grep. But it emails me anyway (with an empty file) even if the grep doesn't return anything.

What should the line be?

Code:
if [ "`grep -l 'unique constraint' $LOGDIR/archive_active* | wc -l`" > "0" ]
then grep -l 'unique constraint' $LOGDIR/archive_active* > $LOGDIR/archive_error.lis
mailx -s "Archive_active logging contains unique constraints - ACTION REQUIRED" john@doe.com <<EOF
`cat $LOGDIR/archive_error.lis`
EOF
fi

thanks in advance.

regards, Meert
# 2  
Old 03-10-2009
use '-gt' instead of '>' for the algebraic comparison.

You could also use '-c' (instead of '-l') for grep and avoid piping to 'wc' - 'man grep'
# 3  
Old 03-10-2009
Try this:
Code:
 grep -q 'unique constraint' $LOGDIR/archive_active* && \
   grep -l 'unique constraint' $LOGDIR/archive_active*  |
   mailx -s "Archive_active logging contains unique constraints - ACTION REQUIRED" \ 
       john@doe.com

# 4  
Old 03-11-2009
Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. UNIX for Dummies Questions & Answers

word count with grep

Hi, It is very interesting to learn the unix, i just struck with a doubt like i have below content in my file xyz xyz xyz xyz i just want know the word count by using grep -wc 'xyz' <filename>, but it is giving 3 instead of 4.So i understood that it is showing matched line numbers count... (2 Replies)
Discussion started by: vmachava
2 Replies

3. 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

4. UNIX for Dummies Questions & Answers

grep word count

How do I do a grep wc that counts occurences of two or more different strings? If I have string1 two times and string2 three times, how do I use wc to get the number 5? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

if, word count

Hi, I need to count the lines of a file stack.html and if the amount lines i want to do something. At this moment, I have if ; then ... This is not working. Any ideas? Thanks! (3 Replies)
Discussion started by: azertyazerty
3 Replies

6. Shell Programming and Scripting

How to grep the where clause of a SQL?

Hi UNIX Gurus, I want to use extract the where clause of a SQL present in a file. Please suggest me how can I do it. Select * from emp where emp_id>10; cat <file_name> | grep -i "where" returns whole SQL. how can I extract only "where emp_id>10;" Thanks in advance (4 Replies)
Discussion started by: ustechie
4 Replies

7. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

8. Fedora

word count wc

could someone explain this please. echo aaaa|wc -c 5 echo aaaa|wc -m 5 But I'd expect the count to be 4 Its SunOS 5.8 Thanks in Advance. (5 Replies)
Discussion started by: chaandana
5 Replies

9. Shell Programming and Scripting

grep all records in a file and get a word count -perl

Hi, I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script thanks (4 Replies)
Discussion started by: meghana
4 Replies

10. UNIX for Dummies Questions & Answers

count word

hi, given a file i need to get the first line and secodn line and count each of the line whether the length of first line and second line is the same i don;t know how to get the length of the line...seems like use 'wc' cannot do it... please advice (1 Reply)
Discussion started by: ariuscy
1 Replies
Login or Register to Ask a Question