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/)

cliffordw 05-24-2012 06:20 AM

Quote:

Originally Posted by cliffordw (Post 4686063)
* The "-v" in the second "grep" command doesn't look right - that would find lines NOT containing the pattern?

Quote:

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

lej 05-24-2012 06:43 AM

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 (Post 4686063)
* 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.

cliffordw 05-24-2012 06:59 AM

Quote:

Originally Posted by lej (Post 4686301)
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.

kosa777777 05-24-2012 09:26 PM

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;

lej 05-25-2012 01:31 AM

Quote:

Originally Posted by cliffordw (Post 4686313)
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?

avijadhav 05-31-2012 08:50 AM

what is means du -sg* commands

avijadhav 07-24-2012 10:53 PM

Aix commands pdf


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