Arrays 从多个数组中提取信息

Arrays 从多个数组中提取信息,arrays,linux,bash,shell,unix,Arrays,Linux,Bash,Shell,Unix,我试图使用多个数组从文件中提取信息,然后echo将该信息输出 我作业中的问题是: •使用/var/log/messages文件,要求用户输入服务名称(例如“syslogd”),并显示特定日期服务重新启动的次数。 因此,例如,如果服务在2014年11月11日重新启动了5次,在2014年11月12日重新启动了3次,您的输出应该类似于以下内容: syslogd服务在11月10日重新启动了5次,时间是:22:00:04 syslogd服务在11月11日重新启动了3次,时间是:22:00:04 我的代码是

我试图使用多个数组从文件中提取信息,然后
echo
将该信息输出

我作业中的问题是:

•使用/var/log/messages文件,要求用户输入服务名称(例如“syslogd”),并显示特定日期服务重新启动的次数。
因此,例如,如果服务在2014年11月11日重新启动了5次,在2014年11月12日重新启动了3次,您的输出应该类似于以下内容:

syslogd服务在11月10日重新启动了5次,时间是:22:00:04

syslogd服务在11月11日重新启动了3次,时间是:22:00:04

我的代码是:

echo -n "Enter the name of the service you wish to log: "; read input
array1=($(grep $input /var/log/messages | awk '{print $1,$2}' | uniq -c | cut -d ' ' -f 4))
array2=$(grep $input /var/log/messages | awk '{print $1,$2}' | uniq | tr -d " ")

for element in "${array1[@]}"
do
    for elementTwo in "${array2[@]}"
    do
        echo "The "$input" service restarted "$element" times on"$elementTwo" "
    done
done
我的输出是:

Enter the name of the service you wish to log: syslogd

The syslogd service restarted 2 times onOct30 Oct31 Nov1 Nov2 Nov3 Nov4 Nov5 Nov6 Nov7 Nov8 Nov9              Nov10 Nov11 Nov12 Nov13 Nov14 Nov15 Nov16 Nov17 Nov18 Nov19 Nov20 Nov21 Nov22 Nov23 Nov24 Nov25 Nov26 Nov27 Nov28 Nov29

The syslogd service restarted 2 times onOct30 Oct31 Nov1 Nov2 Nov3 Nov4 Nov5 Nov6 Nov7 Nov8 Nov9 Nov10 Nov11 Nov12 Nov13 Nov14 Nov15 Nov16 Nov17 Nov18 Nov19 Nov20 Nov21 Nov22 Nov23 Nov24 Nov25 Nov26 Nov27 Nov28 Nov29

The syslogd service restarted 3 times onOct30 Oct31 Nov1 Nov2 Nov3 Nov4 Nov5 Nov6 Nov7 Nov8 Nov9 Nov10 Nov11 Nov12 Nov13 Nov14 Nov15 Nov16 Nov17 Nov18 Nov19 Nov20 Nov21 Nov22 Nov23 Nov24 Nov25 Nov26 Nov27 Nov28 Nov29
/var/log/messages的输出:

Nov 28 08:00:05 opentech syslogd: restart
Nov 28 22:00:04 opentech syslogd: restart
Nov 29 04:00:05 opentech syslogd: restart
Nov 29 20:00:05 opentech syslogd: restart
Nov 29 22:00:07 opentech syslogd: restart

还有更多的输出,但我想你明白了!如您所见,它输出的是所有日期,而不是每行一个。我不知道如何解决这个问题。请帮忙

如果您感兴趣,下面是一个有趣的Perl脚本:

#! /usr/bin/env perl
use warnings;
use strict;

my $service=shift;
my %h;
open (my $fh, "<", '/var/log/messages') or die "Could not open log file: $!\n";
while (<$fh>) {
    if (/$service.*restart/) {
        my($f1,$f2,$f3)=split;
        my $key="$f1 $f2";
        $h{$key}{cnt}++;
        if (exists $h{$key}{arr}) {
            push (@{$h{$key}{arr}}, $f3);
        } else {
            $h{$key}{arr}=[$f3];
        }
    }
}
close($fh);

for (sort keys %h) {
    print "The $service service restarted " . $h{$_}{cnt} . " times on " .
      $_ . ". The times are: " . join (", ", @{$h{$_}{arr}}) . ".\n";
}
输出:

The syslogd service restarted 2 times on Nov 28. The times are: 08:00:05, 22:00:04.
The syslogd service restarted 3 times on Nov 29. The times are: 04:00:05, 20:00:05, 22:00:07.

你能从
/var/log/messages
中给出一些示例行吗?11月28日08:00:05 opentech syslogd:restart 11月28日22:00:04 opentech syslogd:restart 11月29日04:00:05 opentech syslogd:restart 11月29日22:00:07 opentech syslogd:restart
array2
不是一个数组。那就可以了。有没有办法把它变成一个数组?看,昨晚我在想我需要一个for循环,但是我不知道如何把多个参数放进去。我会修改剪切命令。我一直很感兴趣!非常感谢。
The syslogd service restarted 2 times on Nov 28. The times are: 08:00:05, 22:00:04.
The syslogd service restarted 3 times on Nov 29. The times are: 04:00:05, 20:00:05, 22:00:07.