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.

I'm a noob
  • 109
  • 10

1 Answers1

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.

Warren Young
  • 72,032
  • 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 00: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 00: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 00: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 00:53
  • @I'manoob: Okay, rewrote the answer to cover just the csh issue – Warren Young Oct 31 '15 at 00:57
  • @cas: Removed the RE vs glob stuff from the answer, since OP confirmed that his stated pattern is correct. – Warren Young Oct 31 '15 at 00:58
  • +1. you might want to mention why you recommend single quotes: single quotes are for when you have fixed strings, double-quotes are for when you want to interpolate (include the value of) a variable or variables. e.g. 'some string' vs "$HOME/filename.txt" – cas Oct 31 '15 at 01:02