Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to ignore file if search pattern found with a # comment

Status
Not open for further replies.

grazinggoat

Programmer
Mar 12, 2008
41
0
0
US
Hello,


I want to search a directory for a pattern and if its found with a comment to ignore the file
but i want the return code or value. the reason is after checking for the pattern
if its not a commented line i want to grab the lines from the file if its commented I don't want
to know about the file at all.

So lets say file A is in DIR looks like this:

bird
dog
cat

and file B is

#bird
dog
cat

Then good I want to know bird was found in DIR
but if only file B in DIR has #bird I don't want to know
it was there when i data mine for bird.

I want to only know that my search for bird was found in DIR if file A or any other file contain uncommented bird.
doing grep bird DIR/* |grep -v ^# returns 0 because it still finds bird even though it will suppress it.
same with the egrep I am using egrep -h "^[^#].*$h" DIR/*


here is a watered down version what I am doing

for X in `cat list`
do

#See if pattern is in directory to proceed.
FOUND=$( egrep -h "^[^#].*$X" DIR/* )
if [[ $? = 0 ]]
then
#Found X now check each file and print X to template file.
echo "building template"
touch template.txt
for F in `cat flist`
do
grep -h "$X" DIR/$F | grep -v "^#"
if [[ $? = 0 ]]
then
echo "$X Found in DIR/$F
grep $X DIR/$F >> template.txt
else
echo "not found"
continue;
fi
done
else
echo "Not found"
continue;
fi
done

 
I'm having trouble understanding exactly what you're trying to do, but this seems to meet your requirements.

Code:
#!/bin/ksh

ANIMALS="cat bird horse"

[[ -f template.txt ]] && rm template.txt

for X in ${ANIMALS}
do

        grep ${X} DIR/* | grep -v ':#' | while IFS=: read FILE ANIMAL
        do
                print "${ANIMAL} was found in ${FILE}"

                print "${FILE}:${ANIMAL}" >> template.txt
        done
done

print
print "Contents of template.txt"
print "========================"

cat template.txt

print "========================"
print "Done!"

If this isn't what you're looking for, try to give a little more detail. Specifically, what is the desired contents of [tt]template.txt[/tt]?

 
By the way, given your sample files A and B, the script gives the following output...

Code:
cat was found in DIR/A
cat was found in DIR/B
bird was found in DIR/A

Contents of template.txt
========================
DIR/A:cat
DIR/B:cat
DIR/A:bird
========================
Done!

It shows that "cat" was in both files, "bird" was only in one without a comment character, and "horse" wasn't in any of the files.

 
If you need to know if one of the animals wasn't found at all, this would do it...

Code:
#!/bin/ksh

ANIMALS="cat bird horse"

[[ -f template.txt ]] && rm template.txt

for X in ${ANIMALS}
do
        COUNT=0

        grep ${X} DIR/* | grep -v ':#' | while IFS=: read FILE ANIMAL
        do
                print "${ANIMAL} was found in ${FILE}"

                print "${FILE}:${ANIMAL}" >> template.txt

                (( COUNT += 1 ))
        done

        (( ! COUNT )) && print "${X} wasn't found in any files!"
done

print
print "Contents of template.txt"
print "========================"

cat template.txt

print "========================"
print "Done!"

Again, with your sample files A and B, this gives the output...

Code:
cat was found in DIR/A
cat was found in DIR/B
bird was found in DIR/A
horse wasn't found in any files!

Contents of template.txt
========================
DIR/A:cat
DIR/B:cat
DIR/A:bird
========================
Done!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top