grep prints lines that contain a match for a pattern. The general synopsis of the grep command line is grep options pattern input_file_namesThere can be zero or more options. pattern will only be seen as such (and not as an input_file_name ) if it wasn’t already specified within options (by using the ‘ -e pattern ’ or ‘ -f file’ options). There can be zero or more input_file_names.Matching Control
-e pattern--regexp= patternUse pattern as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a ‘ - ’. (-e is specified by POSIX.)-f file--file= fileObtain patterns from file, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)-i-y--ignore-caseIgnore case distinctions, so that characters that differ only in case match each other. Although this is straightforward when letters differ in case only via lowercase-uppercase pairs, the behavior is unspecified in other situations. For example, uppercase “S” has an unusual lowercase counterpart “ſ” (Unicode character U+017F, LATIN SMALL LETTER LONG S) in many locales, and it is unspecified whether this unusual character matches “S” or “s” even though uppercasing it yields “S”. Another example: the lowercase German letter “ß” (U+00DF, LATIN SMALL LETTER SHARP S) is normally capitalized as the two-character string “SS” but it does not match “SS”, and it might not match the uppercase letter “ẞ” (U+1E9E, LATIN CAPITAL LETTER SHARP S) even though lowercasing the latter yields the former.-y is an obsolete synonym that is provided for compatibility. (-i is specified by POSIX.)-v--invert-matchInvert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)-w--word-regexpSelect only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. This option has no effect if -x is also specified.-x--line-regexpSelect only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ‘ ^ ’ and ‘ $’. (-x is specified by POSIX.)Examples:
to find authentication logs for “root” on an debian system:# grep "root" /var/log/auth.logFor example, we can see that when someone failed to login as an admin, they also failed the reverse mapping which means they might not have a valid domain name.# grep -B 3 -A 2 'Invalid user' /var/log/auth.logTo find authentication logs at current system date# grep "$(date +%b) $(date +%e)" /var/log/auth.log | grep 'fail\|preauth'To find authentication logs at current system hour# grep "$(date +%b) $(date +%e) $(date +%H:)" /var/log/auth.logTo find mail logs at current system date# grep "$(date +%b) $(date +%e)" /var/log/mail.infoTo find mail logs at one hour before current system date# grep "$(date --date="1 hours ago" +%b) $(date --date="1 hours ago" +%e)" /var/log/mail.infoA list of date command field descriptors from http://www.cyberciti.biz/faq/unix-linux-bash-get-time/ (as a copy)
References:
- http://www.gnu.org/software/grep/manual/grep.html
- https://www.loggly.com/ultimate-guide/analyzing-linux-logs/
- http://www.cyberciti.biz/faq/unix-linux-bash-get-time/
My Experience Notes These pages contain my experiences using technology. All of the works are working properly at the time when they wrote. You may use them for any purposes.
Tuesday, March 22, 2016
GNU grep
Labels:
debian