INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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

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

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 template.txt?

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

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.

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

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! 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close