I've looked at similar questions and tried most suggestions, but none of them got me anywhere, even though i have a fairly simple task I think :S. I'm fairly new to this whole thing, so I've run out of ideas as to what to try or what I'm doing wrong...

So the task is to get the lines of a file listed (with grep) which contain the short month name and day (like Oct 22). The file has a long format listing in it (containing files created on the current date), so there should be some results. However, I'm getting results in which only the month has been examined and found, the day hasn't.

Some of the more sensible things I've tried so far:

grep $(date +"%b %e") *filename*

Then I get a line saying "grep: 22: No such file or directory" and a listing with all the results for the month ("Oct"). Same results with +%d %e or +'%d %e' or '+%d %e' instead of +"%d %e"; instead of $(). (I also had %d for the day for a while, which made no difference, and shouldn't matter anyway as far as I know, since the date in question was now 22.)

Why is the day not taken into account here?

  • Sputnik's answer explains the error message you had, we can't know why you get no results if you don't show us an example of the file you are running this on.
    – terdon
    Commented Oct 22, 2013 at 23:14

1 Answer 1

grep "$(date +"%b %d")" *files*

should be better.

If you leave out the double quotes, then the result of the command substitution $(date +"%b %d") is split into separate words. Always use double quotes around variable substitutions "$variable" and command substitutions "$(somecommand)" unless you know why you need to leave them out.

  • sorry, the %b %d part was okay, i just mistyped it here, but with the added " " around the date part i got no results at all
    – lunis
    Commented Oct 22, 2013 at 22:34
  • Better gives exact sample input/output in your original post Commented Oct 22, 2013 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .