我有这个输入:
Sep 23 13:43 192.168.6.200
Sep 23 13:44 192.168.6.166
Sep 23 13:45 192.168.6.200
Sep 23 13:46 192.168.6.166
Sep 23 13:47 192.168.6.200
Sep 23 13:48 192.168.6.166
Sep 23 13:49 192.168.6.176
Sep 23 13:49 192.168.6.200
Sep 23 13:50 192.168.6.166
Sep 23 13:51 192.168.6.176
Sep 23 13:51 192.168.6.200
Sep 23 13:52 192.168.6.166
Sep 23 13:54 192.168.6.166
Sep 23 13:54 192.168.6.176
Sep 23 13:56 192.168.6.176
Sep 23 13:57 192.168.6.166
我需要这个输出:
Sep 23 13:43 192.168.6.200
Sep 23 13:51 192.168.6.200
Sep 23 13:44 192.168.6.166
Sep 23 13:57 192.168.6.166
Sep 23 13:49 192.168.6.176
Sep 23 13:56 192.168.6.176
所以我有来自我生成的 OpenWrt 路由器的 DHCP 日志(我将 dhcp 服务器配置为 dhcp 租约为“5m”):
logread | fgrep DHCPACK | awk '{print $1" "$2" "$3" "$8}' | sed 's/:[0-9][0-9] / /g' | sort -u
但我需要提到的输入是提到的输出,怎么办?有perl高手吗? :\ :D
答案1
请注意 awk 是 (大多) fgrep 和 sed 的超集,因此您不需要调用所有三个。
logread | awk '
/DHCPACK/ {
sub(/:..$/,"",$3)
t = $1 " " $2 " " $3
if (!($8 in first)) first[$8] = t
last[$8] = t
}
END {
for (i in first) {
print first[i], i
print last[i], i
print ""
}
}'
尽管它们不会以任何特定顺序出现。如果您希望它们按照 IP 地址首先出现在日志中的顺序显示,您可以将其更改为:
logread | awk '
/DHCPACK/ {
sub(/:..$/,"",$3)
t = $1 " " $2 " " $3
if (!($8 in first)) {
first[$8] = t
ip[n++] = $8
}
last[$8] = t
}
END {
for (i = 0; i < n; i++) {
print first[ip[i]], i
print last[ip[i]], i
print ""
}
}'
答案2
快速破解:
for ip in $(cut -d" " -f 4 < INPUT_FILE.txt | sort -u); do
grep "$ip" INPUT_FILE.txt | head -n1
grep "$ip" INPUT_FILE.txt | tail -n1
echo
done
然而,这不是很快,因为它循环了日志文件 3 次。并且输出的顺序与您的示例不同;我不确定这对你是否重要。
答案3
Perl 版本,带注释。
#! /usr/bin/perl
use strict;
use Date::Parse;
use Date::Format;
my $format = '%b %d %R';
my %IP=();
# read the input file, convert date/time to a time_t timestamp,
# and store it into a hash of arrays (HoA) 'man perldsc' for details
while(<>) {
chomp;
my($month, $day, $time, $ip) = split ;
my $time_t = str2time("$month $day $time");
push @{ $IP{$ip} }, $time_t;
};
# NOTE: hashes are inherently unsorted. replace "keys %IP" below
# with an IP sort function if you need the IPs sorted - e.g.
# http://www.perlmonks.org/?node_id=37889
# print the first and last time stamp seen, converting
# back to the same time format as the input.
foreach my $key (keys %IP) {
# sort the array held in $IP{$key} just in case the input
# lines are out of order
sort @{ $IP{$key} } ;
my $first = $IP{$key}[0];
my $last = $IP{$key}[scalar @{ $IP{$key} }-1];
# print the first and last time seen
# note: if an IP was seen only once, first and last will be the same.
# easy enough to add an if or unless here if you want to exclude
# IPs where first == last.
print time2str($format,$first) . " $key\n";
print time2str($format,$last) . " $key\n\n";
}
如果您想将原始日志通过管道传输到此日志,而不是通过管道传输到 grep、awk 和 sed 后的日志,请进行以下更改。
- 紧接着
while(<>) {
, 添加next unless /DHCPACK/;
将行更改
split
为:my ($month,$day,$year,undef,undef,undef,undef,$IP) = split;
答案4
假设克雷格是正确的,并且您的输入是按时间顺序排列的,awk
可以使用以下惯用脚本:
解析.awk
!start[$4] { start[$4] = $1" "$2" "$3 }
{ end[$4] = $1" "$2" "$3 }
END {
for (ip in start)
print start[ip], ip, "\n" end[ip], ip, "\n"
}
像这样运行:
logread | awk -f parse.awk
输出:
Sep 23 13:43 192.168.6.200
Sep 23 13:51 192.168.6.200
Sep 23 13:44 192.168.6.166
Sep 23 13:57 192.168.6.166
Sep 23 13:49 192.168.6.176
Sep 23 13:56 192.168.6.176