Solved

Position of option changes the behavior in grep...

Posted on 2004-10-15
10
226 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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 62

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

808 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