Solved

Position of option changes the behavior in grep...

Posted on 2004-10-15
10
224 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cant install flashplugins for firefox in freebsd 8 9 44
how to monitor remote shell execution on linux 9 97
Solaris 10.  Nmap installation fails 2 49
Sed question 2 68
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

920 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

12 Experts available now in Live!

Get 1:1 Help Now