このようなログがあるので、文字列Form1Debugging from date [Sun Oct 01 04:26:29.593869 2017] to [Tue Oct 03 04:26:29.593869 2017]
[Tue Oct 03 04:26:29.593869 2017] [:error] [pid 28464]
[client 127.0.0.1:47984] Form1Debugging----- Contact Email ...........
私はこれを試してみましたawk -vDate='date -d'now-48 hours' +[%d/%b/%Y:%H:%M:%S
'$1 > Date {print Date, $0}' error.log
しかし、これらの日付範囲に数千のログがあるため、これは望ましい結果を与えていません。文字列「Form1Debugging」を検索して、指定されたタイムスパンでその文字列のログのみを提供する必要があります。どうすればこれを完了できますか?
推奨される答え に基づいて、簡単なスクリプトを作成しました。スクリプトには5つの入力変数があります。
$1
-日付から(文字列から)$2
-日付(文字列)$3
-ログファイル、フルパスと名前$4
-検索される最初の文字列$5
-検索する2番目の文字列スクリプトの内容は次のとおりです。
#!/bin/bash
# Escape all special characters: "[", "]", ":", " ", "."
s1="$(echo $1 | sed -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/\:/\\\:/g' -e 's/\ /\\\ /g' -e 's/\./\\\./g')"
s2="$(echo $2 | sed -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/\:/\\\:/g' -e 's/\ /\\\ /g' -e 's/\./\\\./g')"
s3="$3"
s4="$(echo $4 | sed -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/\:/\\\:/g' -e 's/\ /\\\ /g' -e 's/\./\\\./g')"
s5="$(echo $5 | sed -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/\:/\\\:/g' -e 's/\ /\\\ /g' -e 's/\./\\\./g')"
[ ! -z "$s5" ] && s5=".*$s5"
# Crop the log file
Sudo sed -n "/$s1/,/$s2/p" "$s3" | grep --color=always "$s4$s5"
スクリプトを呼び出しましょうcrop-log
そして/usr/local/bin
、したがって、シェルコマンドシステム全体として使用可能になります。
Sudo touch /usr/local/bin/crop-log
Sudo chmod +x /usr/local/bin/crop-log
Sudo nano /usr/local/bin/crop-log
nano
で使用します。 Shift+Insert ペースト用; Ctrl+O そして Enter 保存用; Ctrl+X 終了します。使用例:
$ crop-log '[Tue Oct 03 07:35:08.000989 2017]' '04 07:35:07.663281' "/var/log/Apache2/error.log"
[Tue Oct 03 07:35:08.000989 2017] [mpm_prefork:notice] [pid 1622] AH00163: Apache/2.4.18 (Ubuntu) mod_python/3.3.1 Python/2.7.12 OpenSSL/1.0.2g mod_Perl/2.0.9 Perl/v5.22.1 configured -- resuming normal operations
[Tue Oct 03 07:35:08.001011 2017] [core:notice] [pid 1622] AH00094: Command line: '/usr/sbin/Apache2'
[Wed Oct 04 07:35:07.559898 2017] [mpm_prefork:notice] [pid 1622] AH00171: Graceful restart requested, doing restart AH00558: Apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message'
[Wed Oct 04 07:35:07.663176 2017] [:error] [pid 1622] python_init: Python version mismatch, expected '2.7.6', found '2.7.12'.
[Wed Oct 04 07:35:07.663275 2017] [:error] [pid 1622] python_init: Python executable found '/usr/bin/python'.
[Wed Oct 04 07:35:07.663281 2017] [:error] [pid 1622] python_init: Python path being used '/usr/lib/python2.7/:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload'.
$ crop-log 'Oct 03 07:35:08' '04 07:35:07.663281' "/var/log/Apache2/error.log" "[mpm_prefork:notice]"
[Tue Oct 03 07:35:08.000989 2017] [mpm_prefork:notice] [pid 1622] AH00163: Apache/2.4.18 (Ubuntu) mod_python/3.3.1 Python/2.7.12 OpenSSL/1.0.2g mod_Perl/2.0.9 Perl/v5.22.1 configured -- resuming normal operations
[Wed Oct 04 07:35:07.559898 2017] [mpm_prefork:notice] [pid 1622] AH00171: Graceful restart requested, doing restart
$ crop-log '03 07:35:08' 'Oct 04 07:35:07.663281' "/var/log/Apache2/error.log" "[mpm_prefork:notice]" "AH00171:"
[Wed Oct 04 07:35:07.559898 2017] [mpm_prefork:notice] [pid 1622] AH00171: Graceful restart requested, doing restart
$ crop-log '-H--' '-Z--' "/var/log/Apache2/modsec_audit.log" '[id \"'
Message: Access denied with redirection to https://www.youtube.com/watch?v=gLmcGkvJ-e0 using status 302 (phase 2). Match of "ipMatchFromFile /web-security/modsecurity-ip-white.list" against "REMOTE_ADDR" required. [file "/etc/Apache2/mods-enabled/security2.conf"] [line "73"] [id "150"]
Message: Access denied with redirection to https://www.youtube.com/watch?v=nb2evY0kmpQ using status 302 (phase 2). Match of "ipMatchFromFile /web-security/modsecurity-ip-white.list" against "REMOTE_ADDR" required. [file "/etc/Apache2/mods-enabled/security2.conf"] [line "73"] [id "150"]
Message: Access denied with redirection to https://www.youtube.com/watch?v=z9Uz1icjwrM using status 302 (phase 2). Match of "ipMatchFromFile /web-security/modsecurity-ip-white.list" against "REMOTE_ADDR" required. [file "/etc/Apache2/mods-enabled/security2.conf"] [line "73"] [id "150"]