Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 根据日志中的条件计算值_Linux_Shell - Fatal编程技术网

Linux 根据日志中的条件计算值

Linux 根据日志中的条件计算值,linux,shell,Linux,Shell,我有下面这样的日志 11 13/06/2015:00:02:37 10 13/06/2015:00:02:38 7 13/06/2015:00:02:39 10 13/06/2015:00:02:40 1 13/06/2015:00:02:42 1 13/06/2015:00:02:50 1 13/06/2015:00:02:54 2 13/06/2015:23:41:46 1 13/06/2015:23:41:47 2 13/06/2015:23:41:48

我有下面这样的日志

11 13/06/2015:00:02:37
 10 13/06/2015:00:02:38
  7 13/06/2015:00:02:39
 10 13/06/2015:00:02:40
  1 13/06/2015:00:02:42
  1 13/06/2015:00:02:50
  1 13/06/2015:00:02:54
  2 13/06/2015:23:41:46
  1 13/06/2015:23:41:47
  2 13/06/2015:23:41:48
  1 13/06/2015:23:41:49
  2 13/06/2015:23:41:50
  1 13/06/2015:23:41:51
  2 13/06/2015:23:41:52
  1 13/06/2015:23:41:53
  2 13/06/2015:23:41:54
  1 13/06/2015:23:41:55
  1 13/06/2015:23:59:50
  2 13/06/2015:23:59:51
  2 13/06/2015:23:59:52
  2 13/06/2015:23:59:53
  2 13/06/2015:23:59:54
  2 13/06/2015:23:59:55
  1 13/06/2015:23:59:56
  2 13/06/2015:23:59:57
  2 13/06/2015:23:59:58
  2 13/06/2015:23:59:59
我想计算(第1个字段)基于分钟的值,如上图所示,我们看到的是秒数,例如:13/06/2015:23:59:53时的2次计数。我想把它们加起来,以示分寸。请帮助我如何在shell/linux中编写代码

谢谢,

试试这个:

cat path_to_your_log_file|awk '{split($2,res,":");var=res[1]":"res[2]":"res[3];counter[var]+=$1}END{for(i in counter){print counter[i],i}}' 

您可以使用这种方法,使用shell脚本和一些sed:

t=
sum=
f=$(mktemp)
sed -r 's/^ *(.*):[0-9]{2}$/\1/' txt > $f
while read line; do
  c=$(echo "$line"|cut -d' ' -f1)
  currt=$(echo "$line"|cut -d' ' -f2-)
  if [ -n "$t" ] && [ "$t" != "$currt" ]; then
    echo "$sum $t"
    t=$currt
    sum=$c
  elif [ -z "$t" ]; then
    t=$currt
    sum=$c
  else
    sum=$(($sum+$c))
  fi
done < $f
rm -f $f
if [ -n "$t" ] && [ "$t" = "$currt" ]; then
  echo "$sum $t"
fi
t=
总数=
f=$(mktemp)
sed-r的/^*(.*):[0-9]{2}$/\1/'txt>$f
读行时;做
c=$(回声“$线”|切割-d'-f1)
电流=$(回声“$线”|切割-d'-f2-)
如果[-n“$t”]&&[“$t”!=“$currt”];然后
回声“$sum$t”
t=当前美元
总和=$c
elif[-z“$t”];然后
t=当前美元
总和=$c
其他的
总和=$($sum+$c))
fi
完成<$f
rm-f$f
如果[-n“$t”]&&[“$t”=“$currt”];然后
回声“$sum$t”
fi

在shell脚本中执行此操作时,听起来非常复杂。但如果您使用python来执行此操作,则听起来是可以管理的。由于这是一个合适的编程任务,您应该考虑使用适当的编程工具——在几乎每台机器上,至少安装了一个可用的脚本语言,并且在大多数情况下,您可以依赖Perl、Python和其他可能的语言。请解释您的答案。忽略可按“:”分割的最后一个字段
second
,并使用
dd/mm/yyyy:hh:mm
作为计数键。