Linuxでのソートと計算

Linuxでのソートと計算
reboot   system boot  3.10.0-327.el7.x Wed Oct 26 15:12 - 22:43  (07:30)    
root     :0           :0               Wed Oct  5 05:01 - 05:31  (00:29)    
(unknown :0           :0               Wed Oct  5 05:01 - 05:01  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 23:01 - 05:31  (06:29)    
root     :0           :0               Wed Oct  5 04:56 - 04:58  (00:01)    
(unknown :0           :0               Wed Oct  5 04:56 - 04:56  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 22:55 - 04:58  (06:02)  

Linuxで各ユーザーのログイン数を計算し、費やされた合計時間を計算するコマンドを書く方法を教えてくれる人はいますか?このコンテンツを表示する画面に投影してください。

User root logged in a total of ?? times with a total of mins/hours logged
User unknown logged in a total of ?? times with a total of mins/hours logged.

ベストアンサー1

reboot   system boot  3.10.0-327.el7.x Wed Oct 26 15:12 - 22:43  (07:30)    
root     :0           :0               Wed Oct  5 05:01 - 05:31  (00:29)    
(unknown :0           :0               Wed Oct  5 05:01 - 05:01  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 23:01 - 05:31  (06:29)    
root     :0           :0               Wed Oct  5 04:56 - 04:58  (00:01)    
(unknown :0           :0               Wed Oct  5 04:56 - 04:56  (00:00)    
reboot   system boot  3.10.0-327.el7.x Tue Oct  4 22:55 - 04:58  (06:02) 

上記の詳細を考慮すると、ファイルにあります。

for i in `awk '{print $1}' test_login |sort|uniq`;
 do
         count=`grep -c $i test_login`
        sum=0;
        for j in `grep $i test_login|awk '{print $NF}'|cut -c 2-6`;
         do
                 var=`echo $j | awk -F: '{print ($1 * 60) + $2}'`;
                 sum=$((sum+var));
        done
        ((hour=$sum/60))
        ((min=$sum-$hour*60))
        echo "User $i logged in a total of $count times with a total of $hour:$min logged"
  done

test_login:すべてのユーザーログを含むファイル。

おすすめ記事