Helloは以下のようにコマンドプロンプトから行のリストを取得します。
完了しlogread
ていくつかのフィルタを適用した後
Sat Sep 9 07:28:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:29:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:31:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:53 2017 mail.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:34:53 2017 mail.google.com 192.168.150.128
Sat Sep 9 07:35:53 2017 www.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
ここで、過去5秒間に変更された唯一のレコードをインポートしたいと思います。
フィルタコマンドを実行した後は、次の形式が必要です。
そうすることもできますが、logread|awk '$4 > "07:35:00"'
問題は常に時間を殴らなければならないということです。似たようなものがあり、> '5 seconds'
次のようになります。
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
答え1
使用方法については、「テスト」セクションを参照してください。
#!/bin/bash
# select only dates - five columns from the beginning
dates_only=$(cut -d ' ' -f 1-5 "$2")
# the last date is taking - we will calculate the lower time limit from it.
last_line_time=$(tail -n 1 <(echo "$dates_only"))
# generate the unix time (the amount of seconds from 1970) for
# the each date. It will be the additional column in the next.
dates_to_unix_fmt=$(date '+%s' -f <(echo "$dates_only"))
# find the unix time for the last line time.
print_from=$(date '+%s' -d "${last_line_time} - $1")
# adding the unix time in the beginning of the each line.
# it will be removed in the end by the awk.
paste <(printf "%s\n" $dates_to_unix_fmt) "$2" |
awk -v from="$print_from" '$1 > from {sub(/.*\t/, ""); print;}'
テスト
入力(複数行追加 - テスト用)
Sat Sep 9 07:28:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:29:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:31:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:53 2017 mail.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:34:53 2017 mail.google.com 192.168.150.128
Sat Sep 9 07:35:53 2017 www.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
出力
$ ./time_range.sh "10 sec" input.txt
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
$ ./time_range.sh "20 second" input.txt
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
$ ./time_range.sh "4 min" input.txt
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
$ ./time_range.sh "2 minute" input.txt
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
答え2
dat=$(date -d -5sec +%T)
logread|awk -v dat=$dat '$4 > dat'
datを5秒前の時間に設定し、awkに変数として渡します。