Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 Oct 22 '13 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.

share|improve this answer
    
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 Oct 22 '13 at 22:34
    
Better gives exact sample input/output in your original post –  sputnick Oct 22 '13 at 22:38
    
Thanks for the edit Gilles –  sputnick Oct 23 '13 at 10:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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