Solved

Position of option changes the behavior in grep...

Posted on 2004-10-15
10
223 Views
Last Modified: 2010-04-21

grep is weird!

no wait... it's unix, it's supposed to be weird!!!

Anyway, I was just wondering if any other Unixes have the same behavior as this...  (I'm using AIX.)

When I type:
   grep -l "pattern" *

I get the output:
    fileone.ext
    filetwo.ext
    filethree.ext

but when I type:
   grep "pattern" -l *

I get the output:
    fileone.ext: this line contains the word pattern.
    filetwo.ext: this line does not contain the word pattern.... DOH!
    filethree.ext: will this line cause someone to launch a patternity suit?

The man entry for grep only says that the first case should happen.

-- Bubba
0
Comment
Question by:bganoush
10 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 12324041
The options always come first on the Unix command line.

'grep -l pattern *' means to look for 'pattern' in all files in the current directory with the -l option set

'grep pattern -l *' means to look for 'patten' in a file called '-l' plus in all files in the current directory, with no options set
0
 
LVL 11

Expert Comment

by:griessh
ID: 12324288
... and BTW, everything else is weird, UNIX is normal <LOL>
0
 
LVL 11

Expert Comment

by:avizit
ID: 12325730
>> The options always come first on the Unix command line.

need not be always true

you can have

cp -i  file1 file2  
or
cp file1 file2 -i

both of which will do the same thing ,

but yes in the case of grep you are right ...

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12331687
>> The options always come first on the Unix command line.
i.g. not true

to be more precisious on grep:
 standard (AT&T) grep requires options before pattern, while Gnu's grep does not
AIX (and most other proprietary Unix) use standard grep by default
0
 

Author Comment

by:bganoush
ID: 12337457

That's weird... (or normal whatever the case may be...)

If AIX uses "standard" grep, then why did I get the second output???  I don't have a file called "-l"...

>> UNIX is normal

Oh cool.. then I'm normal too!!!  yippeee!  anyone see my socks?

-- Bubba
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 12340696
sometimes AIX seems to be wired and Unix is standard (as my socks are;-)
0
 

Author Comment

by:bganoush
ID: 12340720

Some people wear socks of different colors or socks that are just weird...

But in my case, I sometimes lose count after about 9 or 11... and then I run out of places to hang them...

I also think AIX is wired.. and weird...

-- Bubba
0
 
LVL 61

Accepted Solution

by:
gheist earned 50 total points
ID: 12347662
You should always assume that your UNIX is somewhat POSIX compliant, and not use non-portable extensions of gnu grep awk, bash and so on.
0
 

Author Comment

by:bganoush
ID: 12347895








                                                                 that's weird







0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12351319
gheist, could give a :-( or :-)
i.g. I prefer standards, but POSIX is one of those which gots outdated by reality (like Gnu)
We could argue what''s the better one, but Gnu is there and works, stupid simple, just works ;-.)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now