LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   AIX (https://www.linuxquestions.org/questions/aix-43/)
-   -   Linux command needed? (https://www.linuxquestions.org/questions/aix-43/linux-command-needed-946404/)

Ajit Gunge 05-23-2012 12:32 AM

Linux command needed?
 
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.

divyashree 05-23-2012 12:41 AM

If you will use find to search , it will by default search recursively.

so
Quote:

find <path to xyz> -name zzzz*ttt
Or if this doesnot help, post some examples of file names to get from 1st pattern and then second pattern.

TobiSGD 05-23-2012 04:56 AM

Code:

find /path/to/xyz -name "*zzzz*" | grep ttt
should do the job if I understand you correctly.

pixellany 05-23-2012 05:02 AM

find <path> -name "xyz" looks for "xyz" in the filename. I think maybe OP wants to look in the file content?

Ajit Gunge 05-23-2012 05:17 AM

Yes I want to search the second pattern in the content of the files that are obtained from the result of the first pattern.

pixellany 05-23-2012 05:35 AM

Quote:

Originally Posted by Ajit Gunge (Post 4685481)
Yes I want to search the second pattern in the content of the files that are obtained from the result of the first pattern.

Still ambiguous.....Are BOTH search patterns to be found in the file content (not the filename)?

Ajit Gunge 05-23-2012 06:32 AM

Pixellany,
Yes both the patterns are to be found in the file contents.

pixellany 05-23-2012 08:46 AM

Try this:

Looking in filename for "xxx", then--where "xxx" is found--look for "yyy":

Code:

grep xxx filename | grep yyy
this works line by line---i.e. when it finds "xxx" on a line, then it looks for "yyy" on the same line

TobiSGD 05-23-2012 09:27 AM

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


salasi 05-23-2012 09:40 AM

Quote:

Originally Posted by TobiSGD (Post 4685625)
...not tested, currently no Linux available...

That's ok, this thread is in >Other *NIX Forums > AIX, so presumably the OP wants to do this under AIX :D

Ajit Gunge 05-23-2012 09:48 AM

TobiSGD,
Thanks for the script but I would always have to edit the script to give the inputs.

TobiSGD 05-23-2012 10:44 AM

Quote:

Originally Posted by Ajit Gunge (Post 4685638)
TobiSGD,
Thanks for the script but I would always have to edit the script to give the inputs.

No, you wouldn't. You can call the script like this:
Code:

./scriptname /path/to/search/in first_match second_match
Assuming you name the script find_matches it would like this for the example you gave use:
Code:

./find_matches xyz zzzzzz ttt

cliffordw 05-24-2012 12:17 AM

Quote:

Originally Posted by TobiSGD (Post 4685625)
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

Hope this helps :-)

kosa777777 05-24-2012 02:40 AM

"find" is your friend

Code:

find /tmp -name "xyz"
Check this post explaining more about find with examples:

http://thetechtavern.com/2012/05/13/...o-find-a-file/

TobiSGD 05-24-2012 05:53 AM

Quote:

Originally Posted by cliffordw (Post 4686063)
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:

http://thetechtavern.com/2012/05/13/...o-find-a-file/

In this case find will not help, the OP is searching for patterns in the file's content, not in the filename.


All times are GMT -5. The time now is 05:17 PM.