LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices

Reply
 
Search this Thread
Old 05-23-2012, 12:32 AM   #1
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Question 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.
 
Old 05-23-2012, 12:41 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,349

Rep: Reputation: 135Reputation: 135
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.
 
Old 05-23-2012, 04:56 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046
Code:
find /path/to/xyz -name "*zzzz*" | grep ttt
should do the job if I understand you correctly.
 
Old 05-23-2012, 05:02 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
find <path> -name "xyz" looks for "xyz" in the filename. I think maybe OP wants to look in the file content?
 
Old 05-23-2012, 05:17 AM   #5
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Yes I want to search the second pattern in the content of the files that are obtained from the result of the first pattern.
 
Old 05-23-2012, 05:35 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by Ajit Gunge View Post
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)?
 
Old 05-23-2012, 06:32 AM   #7
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Original Poster
Rep: Reputation: 21
Pixellany,
Yes both the patterns are to be found in the file contents.
 
Old 05-23-2012, 08:46 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 05-23-2012, 09:27 AM   #9
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046
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

Last edited by TobiSGD; 05-23-2012 at 09:28 AM.
 
Old 05-23-2012, 09:40 AM   #10
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,902

Rep: Reputation: 775Reputation: 775Reputation: 775Reputation: 775Reputation: 775Reputation: 775Reputation: 775
Quote:
Originally Posted by TobiSGD View Post
...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
 
Old 05-23-2012, 09:48 AM   #11
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Original Poster
Rep: Reputation: 21
TobiSGD,
Thanks for the script but I would always have to edit the script to give the inputs.
 
Old 05-23-2012, 10:44 AM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046
Quote:
Originally Posted by Ajit Gunge View Post
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
 
1 members found this post helpful.
Old 05-24-2012, 12:17 AM   #13
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 275

Rep: Reputation: 118Reputation: 118
Quote:
Originally Posted by TobiSGD View Post
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 :-)
 
1 members found this post helpful.
Old 05-24-2012, 02:40 AM   #14
kosa777777
LQ Newbie
 
Registered: May 2012
Posts: 7

Rep: Reputation: Disabled
"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/
 
1 members found this post helpful.
Old 05-24-2012, 05:53 AM   #15
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046Reputation: 4046
Quote:
Originally Posted by cliffordw View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dd command help needed. pinga123 Linux - Newbie 1 05-05-2011 10:57 PM
linpus linux lite v.1.0.3.E konsole command list and tutorial needed glenellynboy Linux - Laptop and Netbook 4 04-10-2010 08:00 AM
dd command help needed here neocontrol Linux - General 3 10-12-2008 10:18 AM
Help needed for find command ZAMO Linux - General 7 09-23-2008 04:15 AM
linux Command needed cherupop Linux - Newbie 4 07-05-2005 11:33 PM


All times are GMT -5. The time now is 07:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration