bash - Shell 脚本获取两年之间的值

标签 bash shell awk

我正在使用一个运行良好的小型 bash 代码,但我只是想看看是否有更好的方法来形式化 awk 片段,即下面的片段,它正在查找第八列上 2002 年至 2018 年之间的文件.

$AWK" '$8 >= "2002" && $8<= "2018"' "$FILE

脚本:

#!/bin/bash

FILE="/home/pygo/Cyberark/ftplogs_3"
AWK="/bin/awk"
GREP="/bin/grep"
USERS="`"$AWK" '$8 >= "2002" && $8<= "2018"' $FILE | "$AWK" '{print $3}' | sort -u`"
for user in $USERS;
do
echo "User $user " | tr -d "\n";
"$AWK" '$8 >= "2002" && $8<= "2018"' "$FILE" | "$GREP" "$user" | "$AWK" '{ total += $5}; END { print "Total Space consumed: "  total/1024/1024/1024 "GB"}';
done | column -t
echo ""
echo "=============================================================="
"$AWK" '$8 >= "2002" && $8<= "2018"' "$FILE" | "$AWK" '{ total += $5}; END { print "Total Space consumed by All Users: "  total/1024/1024/1024 "GB"}';
echo ""

结果:

User  16871                   Total  Space  consumed:  0.0905161GB
User  253758                  Total  Space  consumed:  0.0750855GB
User  34130                   Total  Space  consumed:  3.52537GB
User  36640                   Total  Space  consumed:  0.55393GB
User  8490                    Total  Space  consumed:  3.70858GB
User  tx-am                   Total  Space  consumed:  0.18992GB
User  tx-ffv                  Total  Space  consumed:  0.183137GB
User  tx-ttv                  Total  Space  consumed:  17.2371GB
User  tx-st                   Total  Space  consumed:  0.201205GB
User  tx-ti                   Total  Space  consumed:  58.9704GB
User  tx-tts                 Total  Space  consumed:  0.0762068GB

------------ snipped output --------------
==============================================================
Total Space consumed by All Users: 255.368GB

编辑:示例数据

-rw-r--r-- 1 34130 ftpsecure 101M Mar 26  2007 /data1/focus-del/files_1
-rw-r--r-- 1 34130 ftpsecure 172M Oct 13  2005 /data1/focus-del/files_2
-rw-r--r-- 1 34130 ftpsecure 213M Nov  8  2005 /data1/focus-del/files_3
-rw-r--r-- 1 34130 ftpsecure 138M Feb 17  2006 /data1/focus-del/files_4
-rw-r--r-- 1 34130 ftpsecure 169M Sep 26  2016 /data1/focus-del/files_5
-rw-r--r-- 1 34130 ftpsecure 214M Nov 15  2018 /data1/focus-del/files_6
-rw-r--r-- 1 34130 ftpsecure 101M Mar 26  2002 /data2/focus-del/files_1
-rw-r--r-- 1 34130 ftpsecure 172M Oct 13  2006 /data2/focus-del/files_2
-rw-r--r-- 1 34130 ftpsecure 213M Nov  8  2008 /data2/focus-del/files_3
-rw-r--r-- 1 34130 ftpsecure 138M Feb 17  2016 /data2/focus-del/files_4
-rw-r--r-- 1 34130 ftpsecure 169M Sep 26  2018 /data2/focus-del/files_5
-rw-r--r-- 1 34130 ftpsecure 214M Nov 15  2018 /data2/focus-del/files_6

预期输出:

$ sh Ftp_cal.sh
User  34130  Total  Space  consumed:  1.87568e-06GB

==============================================================
Total Space consumed by All Users: 1.87568e-06GB

最佳答案

您可以使用此awk脚本:

awk '$8 >= 2002 && $8 <= 2018 {
   sum[$3] += $5
}
END {
   for (i in sum) {
      printf "User  %s  Total  Space  consumed: %d\n", i, sum[i]
      total += sum[i]
   }
   print "==============="
   print "Total Space consumed by All Users:", total
}' file

User  34130  Total  Space  consumed: 2014
===============
Total Space consumed by All Users: 2014

关于bash - Shell 脚本获取两年之间的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55748115/

相关文章:

bash - zsh vi 模式状态行

python - crontab 没有完全工作。仅运行 echo 语句

C: main() 返回一个数组总是等于 56

linux - linux下如何查找字符串

linux - Bash 匹配多行字符串并附加到上一行

python - open() 中的整数文件描述符 "0"

linux - ./配置 :/bin/sh^M : bad interpreter

linux - 在每个文件夹中制作文件夹和文件的脚本

linux - UNIX:如何将IP地址转换为二进制代码

批处理脚本中的 AWK 等效项