1833790 Members
2725 Online
110063 Solutions
New Discussion

Re: Pattern Searching

 
SOLVED
Go to solution
roadrunner_1
Regular Advisor

Pattern Searching

I need to look for a particular pattern. This pattern occures in many files in different directories....

For example;
Iam looking for disabled accounts in a HP trusted host. These accounts are found in /tcb/files/auth/
I want to search for 'u_lock' in the various files in the directories under /tcb/files/auth

Can anybody please give me a solution since my knowledge of scripting is bad
7 REPLIES 7
Tom Maloy
Respected Contributor

Re: Pattern Searching

From the command line with find, you can use exec or pipe to xargs:

find /tcb/files/auth -type f -exec grep u_lock {} \;

OR

find /tcb/files/auth -type f | xargs grep u_lock


That xargs might need some other parameter...

Tom
Carpe diem!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Pattern Searching

This will get you started but it will only work on text files:

#!/usr/bin/sh
TARGET="${1}"
shift

find . -type f | while read X
do
grep -q "${TARGET}" ${X}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "File: ${X}"
grep "${TARGET}" ${X}
fi
done

Use it like this:
1) cd to desired directory
2) find.sh u_lock

This will then descend the filetree at that point and list all files which contain the target phrase and the lines.
If it ain't broke, I can fix that.
steven Burgess_2
Honored Contributor

Re: Pattern Searching

find /tcb/files/auth -name '*' -exec grep -i -l u_lock' {} \; > /tmp/output_file

This will find all files contained within the /tcb/files/auth directory (and any subdirectories). Notice that the wildcard * is enclosed in single quotes. The output of the find is sent to a grep with a -i (case insensitive) and -l (ell) switch. The -l swich is necessary as this causes the grep to return the name of the file in which the search text was found (as opposed to simply returning a copy of the line itself).

In the above example, I have searched for the string 'u_lock' (note the single quotes again around the string) and sent the output to a file called /tmp/output_file.

HTH

Steve
take your time and think things through
Sajid_1
Honored Contributor

Re: Pattern Searching

You can just go to that directory and do a simple grep:
# cd /tcb/files/auth
# cat * | grep "u_lock"

OR for a complete search on any directory:
# cd dir
# find . -type f -depth -print -exec grep "u_lock" {} \;
learn unix ..
Martin Johnson
Honored Contributor

Re: Pattern Searching

If you do it this way:

find . -type f -depth -exec grep "u_lock" {} \; -print

It prints only the files that had a successful grep, not all the files checked.

HTH
Marty
roadrunner_1
Regular Advisor

Re: Pattern Searching

Thanks for all your help. I have awarded points for the same....

The best answers which solved 100% of my requirement has been awarded 10 points...

NO POINTS WILL BE AWARDED TO THE ANSWERS POSTED HEREAFTER....
Wodisch_1
Honored Contributor

Re: Pattern Searching

Hi roadrunner,

be careful with those "grep"s onto binary files!

cd /dir-to-start-from
find . -type f -print |
while read name; do
case "$(file $name)" in
*text*) grep "u_lock" $name /dev/null ;;
esac
done

This will only try to "grep" onto textfiles... Imagine running one of the *other* find/grep combinations, and there would be a file containing the binary codes to lock your keyboard close to "u_lock" (or some even more dangerous combinations, like instructing your terminal to *believe* you have just entered "rm -rf /* \n" or such)!

N/A=0 points, please (as announced), but I do want you to be careful ;-)

Wodisch