Shell脚本采用介于两年之间的值

时间:2019-04-18 14:24:53

标签: 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

1 个答案:

答案 0 :(得分:3)

您可以使用以下from oauth2client import client, file def get_service(): flow = client.flow_from_clientsecrets(CLIENT_SECRETS_FILE, SCOPES) storage = file.Storage(API_SERVICE_NAME + '.dat') credentials = storage.get() http = credentials.authorize(http=httplib2.Http()) service = build(API_SERVICE_NAME, API_VERSION, http=http) return service 脚本:

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