AIXThis forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am into a folder say xyz which has miltiple files/folders.Now I want to find all the files recersively that contain a particular pattern "zzzz" first and later another patter "ttt" on the result obtained from the earlier pattern using the find command.I am on an AIX system.The search should be fast enough to give me the results.Please help.
I would do it this way (small script, not tested, currently no Linux available):
Code:
#!/bin/zsh
# The script has to be started with $1 = path to search in, $2 = first match, $3 = second match
# No exception handling, if you want it add it.
#
# First find all files that contain that first match
FIRST=$(grep -rl $2 $1/*)
# Now search for the second match in those files
for i in $FIRST
do
if [ grep -v $3 $i ]
then
echo $i
fi
done
I would do it this way (small script, not tested, currently no Linux available):
Code:
#!/bin/zsh
# The script has to be started with $1 = path to search in, $2 = first match, $3 = second match
# No exception handling, if you want it add it.
#
# First find all files that contain that first match
FIRST=$(grep -rl $2 $1/*)
# Now search for the second match in those files
for i in $FIRST
do
if [ grep -v $3 $i ]
then
echo $i
fi
done
My I suggest a few minor changes to the above script, namely:
* Change the shell for AIX (as per OP)
* Replace the filename wildcard to avoid "parameter list too long" errors
* Quote arguments and file names to handle spaces in either
* The "-v" in the second "grep" command doesn't look right - that would find lines NOT containing the pattern?
The modified script would look like this:
Code:
#!/usr/bin/ksh
# The script has to be started with $1 = path to search in, $2 = first match, $3 = second match
# No exception handling, if you want it add it.
#
# First find all files that contain that first match
find "$1" -type f -print | xargs grep "$2" | while read i
do
# Now search for the second match in those files
if [ grep "$3" "$i" > /dev/null ]
then
echo "$i"
fi
done
My I suggest a few minor changes to the above script, namely:
* Change the shell for AIX (as per OP)
I don't know which shell is standard on AIX, so I just used my favorite shell shell.
Quote:
The "-v" in the second "grep" command doesn't look right - that would find lines NOT containing the pattern?
Exactly. This makes the if statement a little bit easier. By default grep exits with exit-code 0 when it finds a match and with exit-code 1 when it can't find a match. Testing the exit-code with this behavior for matches makes it necessary to have an else-branch in the statement. The -v option inverts this behavior, so that grep exits with 1 when finding a match. This way we don't need an else branch.
Quote:
Originally Posted by kosa777777
"find" is your friend
Code:
find /tmp -name "xyz"
Check this post explaining more about find with examples:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.