Solved

Position of option changes the behavior in grep...

Posted on 2004-10-15
10
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
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.

705 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