Reputation: 1
I'm trying use grep command to search a large log file for lines starting with current date through server automation application and the date format written in log file is 'Month Date'.
The limitation is we cannot use double quotes in the command while running it from this application.
I tried to grep the file with below command, but it's not working since it is replacing the date variable with space in between month and date, and due to that grep is treating month as the word to search and date as file.
grep `date +%b\ %d` filename
Can someone help me fix this, so the date variable gets replaced with single quotes, for example Oct 30
, so grep can treat it as single word for searching?
Please note we cannot use double quotes in entire command.
Upvotes: 0
Views: 116
Reputation: 531055
You can set IFS
to a character that doesn't appear in the output of date
; a colon should suffice. Localize the change in a subshell to avoid needing to restore the previous value.
(IFS=:; grep $(date +%b\ %d) file)
Upvotes: 0
Reputation: 148
grep `date +%b.*%d` largelog.log
This works unless you have 30 after Oct anywhere in your log line.
Upvotes: 0
Reputation: 88583
Try this:
grep `date +%b[[:space:]]%d` filename
or
grep $(date +%b[[:space:]]%d) filename
Upvotes: 2