Getting a date format with grep
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.
Related videos on Youtube
lunis
Updated on September 18, 2022Comments
-
lunis almost 2 years
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?
-
terdon over 10 yearsSputnik'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.
-
-
lunis over 10 yearssorry, 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
-
Gilles Quenot over 10 yearsBetter gives exact sample input/output in your original post