1
ssh1% cat temp

sarah

saab

marrrrrrrrra

marrrrrrrtha

ssh1% grep ar*a temp

grep: No match.

Shouldn't this be the output?

sarah saab marrrrrra

I tried using putty and nxclient for Windows 10.

  • 2
    What is the output of ls ar*a? – Mark Wagner Oct 30 '15 at 23:21
  • No match. The only files I have in this directory are Jabberwocky, temp and test. Someone else answered it but thanks! – I'm a noob Oct 31 '15 at 0:51
3

You're using csh, which sees the * and tries to expand it as a shell glob and fails. That is, it's csh saying "No match" here, not grep. Unlike Bourne family shells, csh won't pass an unexpandable glob pattern on to the command, so you must quote it to get csh to release it to grep.

I recommend single quotes here since you aren't trying to interpolate a variable into the string:

% grep 'ar*a' temp
sarah
saab
marrrrrrrrra

Double quotes will also work in this case because your pattern doesn't contain $, but I recommend using single quotes here anyway. It's a habit worth cultivating to carefully consider which quotes you use in langauges that let you use them semi-interchangeably, such as shell, Perl, etc. We don't need string interpolation here, so we will choose not to use the quoting style that allows it.

You should quote RE patterns under Bourne family shells, too, by the way. As Mike Wagner hints in his comment above, because regular expressions and glob patterns use many of the same special characters, you can sometimes write an RE pattern that just happens to also match some files, and is thus unexpectedly expanded by the shell.

  • ar*a matches saab. * is zero-or-more. so a followed by zero-or-more rs, followed by another a. The OPs expected output is correct, or would be if he properly quoted the regexp to prevent shell glob expansion. grep 'ar*a' temp – cas Oct 31 '15 at 0:45
  • ssh1% grep ar.*a temp grep: No match. and ssh1% grep 'ar*a' temp sarah saab marrrrrrrrra are my results. Why does this not work without the quotes? – I'm a noob Oct 31 '15 at 0:46
  • @I'manoob: I've edited the answer to explain No match from csh, which is what's really going on here, not the glob vs RE issue I originally assumed. – Warren Young Oct 31 '15 at 0:52
  • Your pattern says a possibly followed by some number of rs, followed by another a. Is that what you actually mean to find? Yes this is what I'm trying to do. – I'm a noob Oct 31 '15 at 0:53
  • @I'manoob: Okay, rewrote the answer to cover just the csh issue – Warren Young Oct 31 '15 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.