3

I am using the below command in solaris

ls -l | grep '*PROC*'

But it is not working. I have many files that contain "PROC" in their name like "XREF_PROC.complete" but when I use the above command its not showing any output. When I use ls *PROC* its working but not working with grep.

Thanks

sandywho
  • 33
  • 3

1 Answers1

10

tl;dr

You don't need a quantifier, just grep for PROC:

ls | grep PROC

long version

The asterisk in your ls line, is not the same as the one in your grep line.

When you have an unescaped asterisk on the command line, the shell will expand it before ls sees it, this is called globbing. An asterisk alone expands to all files in the current directory, try for example echo * . The *PROC* glob will expand to all files containing PROC.

The asterisk you are using with grep is escaped with single-quotes, and thus will be interpreted by grep. However, grep uses BREs by default (Basic Regular Expressions), where the asterisk works as a quantifier, and so requires some character or character class to quantify, e.g. '.*'.

barlop
  • 23,380
  • 43
  • 145
  • 225
Thor
  • 6,419
  • 1
  • 36
  • 42
  • Thanks for the help. I use REDHAT unix servers and when I do `ls -l | grep *PROC*` it works fine. But not in solaris. So is this a unique case in solaris or am I missing somethinh – sandywho Jul 24 '15 at 10:25
  • 2
    @sandywho: Notice that you are _not_ escaping the asterisks, this means the shell will expand `*PROC*` before grep sees it. This *only* works if only one file exists with PROC in its name. Always escape patterns given to grep! – Thor Jul 24 '15 at 10:35
  • 1
    you know this but it's worth mentioning 'cos your answer specifically brings up BREs., Grep can use EREs or PCREs too, though indeed in all of these, any regular expression, it'd be `.*` as with BREs. So it's not an issue of BRE/ERE/PCRE it's just the fact that it's a Regular Expression. – barlop Jul 24 '15 at 11:04
  • 1
    @sandywho type `echo *` see if the output is different in solaris, and how it difers. `*` will expand to every file in the current directory (not exactly what you want but that's what * is doing when interpreted by the *nix shell). It's not like in the windows command line where the command always interprets the `*`. Try `ls -l | grep '.*PROC.*'` 'cos `.*` is regex talk for any sequence of characters – barlop Jul 24 '15 at 11:07
  • @barlop In other words, if a command like `echo *` would take long enough to run (and were not an internal), you could find it in `ps x` and it would show up there as `echo file1 file2 file3 file4` instead of `echo *` – Hagen von Eitzen Jul 24 '15 at 16:38
  • @sandywho : Redhat is not certified unix, it's unix-like OS, linux – rkosegi Jul 24 '15 at 17:00
  • @HagenvonEitzen yeah but that 'complifies' my point. though since my point was just that * expands, echo was really merely a safety thing, I could've simplified my point more than echo, but saying type just * enter i.e. . `$*` but then if the first file was a command it'd execute it, so echo is safer – barlop Jul 24 '15 at 19:02