LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 05-24-2012, 06:20 AM   #16
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 247

Rep: Reputation: 106Reputation: 106

Quote:
Originally Posted by cliffordw View Post
* The "-v" in the second "grep" command doesn't look right - that would find lines NOT containing the pattern?
Quote:
Originally Posted by TobiSGD View Post
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.
The -v option doesn't invert the return code, it inverts the matching logic (i.e. find lines that don't contain the pattern). It will return 0 if it finds lines that don't contain the pattern, and 1 if ALL lines contain the pattern. This is not what is required here. Your understanding of the if statement also seems flawed - it executes the "then" code if the exit code was 0, and the "else" code if not :-)

My version also had a few bugs, though. My first grep should have had a -l parameter to list the files only, rather than show the matching lines. The syntax of my if statement is also incorrect. Sorry, I don't usually use this syntax :-) The square brackets (common syntax for the test built-in command) do not belong there.

The corrected script (tested on AIX this time) is as follows:
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
rc=1
find "$1" -type f -print | xargs grep -l "$2" | while read i
do
    # Now search for the second match in those files
    if grep -q "$3" "$i"
    then
        echo "$i"
        rc=0
    fi
done
exit $rc
I've added an exit code with the same behaviour as grep, i.e. it returns zero if a matching file was found, and 1 if not.

Last edited by cliffordw; 05-24-2012 at 06:33 AM.
 
Old 05-24-2012, 06:43 AM   #17
lej
LQ Newbie
 
Registered: Aug 2011
Location: UK
Distribution: Slackware
Posts: 29

Rep: Reputation: Disabled
If you just want a list of the files that contain the pattern, then use the -l option:

Code:
grep -l <pattern> <files>
Also, btw,

Quote:
Originally Posted by cliffordw View Post
* Quote arguments and file names to handle spaces in either
TobiSGD used zsh which by default does not do automatic word splitting (a completely retarded concept), so parameters do not need to be quoted.
 
Old 05-24-2012, 06:59 AM   #18
cliffordw
Member
 
Registered: Jan 2012
Location: South Africa
Posts: 247

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by lej View Post
TobiSGD used zsh which by default does not do automatic word splitting (a completely retarded concept), so parameters do not need to be quoted.
Ah, my oversight - sorry TobiSGD. I've never used zsh - will add it to my list of things to check out when time permits:-) I personally prefer bash for interactive use, but use ksh for scripts to maximize portability.
 
Old 05-24-2012, 09:26 PM   #19
kosa777777
LQ Newbie
 
Registered: May 2012
Posts: 7

Rep: Reputation: Disabled
Oh.. my bad I misunderstood that, here is a one liner then:

Code:
for i in `grep -R "zzz" * | cut -d ":" -f1`;do echo $i:; egrep "xxx" $i;done;
 
Old 05-25-2012, 01:31 AM   #20
lej
LQ Newbie
 
Registered: Aug 2011
Location: UK
Distribution: Slackware
Posts: 29

Rep: Reputation: Disabled
Quote:
Originally Posted by cliffordw View Post
Ah, my oversight - sorry TobiSGD. I've never used zsh - will add it to my list of things to check out when time permits:-) I personally prefer bash for interactive use, but use ksh for scripts to maximize portability.
Like TobiSGD, zsh is also my preferred shell and it borrows many features from
csh and ksh (including autoloaded functions). If you have experience with ksh
you should find it quite comfortable. It is (in my entirely biased opinion)
superior to bash both for scripting and interactive use. It has far too many
features to go into (e.g. nested parameter expansion, csh-style paramter
modifiers, floating point, associative arrays (hash tables), command line
stack (push a half-completed command line, type another command (e.g. to check
a man page etc), then the other line is popped back ready to be completed),
some nice history handling options (e.g. remove duplicate history entries),
dynamically loadable modules, programmable context-sensitive tab completion
(it ships with completion functions for hundreds of commands), and that's just
scratching the surface (the man pages (there are about a dozen) total about 4
or 5 times the size of bash's man page, which gives you some indication of the
quantity of features (of course, some people read 'features' as 'bloat'!)). I
certainly regard it as a step up from sh/bash in terms of expressiveness and
functionality.

Many (most?) Linux users just seem to accept bash as it is the default but it can
be profitable to explore the other options. I bounced from bash to ksh to csh to
tcsh before finally settling on zsh. There are also many other shells of course,
like the scheme shell (scsh), but I do not have any experience with that
(yet).

Zsh may not be everyone's cup of tea but it's certainly worth taking a look
at. Be warned though, it is very configurable and has many options (about 170
or so) and depending on one's preferences, may need a week or two to configure
to one's liking. I've been using zsh more or less exclusively for over a
decade and I'm still tweaking!

edit:
I'm still unclear on what output the OP wants, is it the matching content itself, or a list of the files that have matching content?

Last edited by lej; 05-25-2012 at 01:36 AM.
 
Old 05-31-2012, 08:50 AM   #21
avijadhav
LQ Newbie
 
Registered: May 2012
Posts: 11

Rep: Reputation: Disabled
what is means du -sg* commands
 
Old 07-24-2012, 10:53 PM   #22
avijadhav
LQ Newbie
 
Registered: May 2012
Posts: 11

Rep: Reputation: Disabled
Aix commands pdf
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 10:01 AM.

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