LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Grep Issues (https://www.linuxquestions.org/questions/linux-software-2/grep-issues-4175611544/)

glennbtn 08-08-2017 03:16 AM

Grep Issues
 
Hi All

I am running debian 8.5 and seem to be having issues getting data with grep.

The command I am trying to run is grep "^$(date --date -2hour +'%Y-%m-%d %H:%M')" /var/log/logfilename | grep 'auth failure'

This just returns a blank line. If I run (date --date -2hour +'%Y-%m-%d %H:%M') this retuns the correct time -2 hours as expected

If I run grep "auth failure' /var/log/logfilename it returns the data expect but clearly the whole lot from the log file.

So can anyone advise what I am doing wrong to get nothing when I run the whole command.

Thanks

Glenn

pan64 08-08-2017 03:41 AM

probably try grep -F and without ^

glennbtn 08-08-2017 03:48 AM

Many thanks for the suggestion.

Tried but still no result

pan64 08-08-2017 03:53 AM

would be nice to show us some examples (lines to check).

syg00 08-08-2017 04:03 AM

And if you run the first grep command, what do you get ?.
Diagnostics 101.

aragorn2101 08-08-2017 04:14 AM

Have you tried only
Code:

grep "$(date --date -2hour +'%Y-%m-%d %H:%M')" /var/log/logfilename
and see what it gives you.

michaelk 08-08-2017 04:20 AM

Try not using quotes around the date command.

Make sure the output of your date command is not missing any extra spaces etc.

descendant_command 08-08-2017 05:18 AM

Well you're only going to get a match if you actually had an auth failure message logged during that one minute two hours ago.

scasey 08-08-2017 07:23 AM

Quote:

Originally Posted by glennbtn (Post 5745856)
Code:

grep "^$(date --date -2hour +'%Y-%m-%d %H:%M')"

"^" is a start of line anchor, "$" is the end of line anchor; grep will ignore the stuff after that... you need to
Code:

grep "\^\$\(date --date -2hour +'%Y-%m-%d %H:%M'\)"
to grep for what you want, then what descendant_command said.
I wasn't able to make a match in a log file at all with that syntax, tho. I'm not sure you can build your date to match on the fly like that.

If you're trying to find an instance of auth_failure at a specific time,
put the results of the date command into a variable, then use it in the grep
Code:

tmpdt=$(date --date -2hour +'%Y-%m-%d %H:%M');grep 'auth failure' /var/log/logfilename | grep "$tmpdt"
1. store the date-time in $tmpdt
2. grep for 'auth failure'
3. grep for the date-time
...but then what descendant_command said

MadeInGermany 08-09-2017 02:05 AM

Assuming this is a shell command, the previous saying is not true, because the shell substitutes the $( ) before the grep is invoked.
Rather I think there is no matching log entries at the given minute. Try to omit the %M and look at the matches (for the full hour)!


All times are GMT -5. The time now is 04:57 PM.