Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Sep 2010
How to tell if grep didn't return anything
I have a file:
I want to see if all the records in the file are present in the contents of the files of a particular directory.
Basically I want to say if grep doesn't return anything, then report.
For example in /tmp dir I have 4 files and flast 2 values (787862348 and 766428634) are present in the files of /tmp dir, but first one (979798707) is not. I want to echo that in a reporting file.
while read line
# if ! grep -rl $line /tmp
echo $line >> are_not_present
done < "myFile"
How do I achieve " if ! grep -rl $line /tmp"? That is, if the line is found by grep, then grep will print the output, but if grep does'nt find it, it will print nothing. How can I check if grep didn't find it (i.e. printed nothing)?
- Join Date
- Mar 2009
Check the return status ($?)
When grep finds something return status is 0
When grep doesn't find something return status is 1
how about something like:
for line in $(cat file); do for file in $(ls /tmp); do if [ -z "(grep $line /tmp/$file)" ]; then echo "I couldn't find $line in /tmp/$file"; else echo "I found $line in /tmp/$file"; fi done done
for line in $(cat file); do for file in $(ls /tmp); do if [ ! -z "(grep $line /tmp/$file)" ]; then echo "I found $line in /tmp/$file"; fi done done
Last edited by stokes; 03-15-2011 at 08:30 PM.Registered Linux user #389109
My Semi-Linux Blog