I'm working with a small bash code which is working fine but i'm just looking if there is better way to formulate this, In this code i'm looking for the Files between year 2002 and 2018 on the 7th column.
Below is the working code,
Script:
#!/bin/bash
# scriptName: Ftpcal.sh
FILE="/home/pygo/Cyberark/ftplogs_3"
AWK="/bin/awk"
GREP="/bin/grep"
USERS="`"$AWK" '$7 >= "2002" && $7 <= "2018"' $FILE | "$AWK" '{print $3}' | sort -u`"
for user in $USERS;
do
echo "User $user " | tr -d "\n";
"$AWK" '$7 >= "2002" && $7 <= "2018"' "$FILE" | "$GREP" "$user" | "$AWK" '{ total += $4}; END { print "Total Space consumed: " total/1024/1024/1024 "GB"}';
done | column -t
echo ""
echo "=============================================================="
"$AWK" '$7 >= "2002" && $7 <= "2018"' "$FILE" | "$AWK" '{ total += $4}; END { print "Total Space consumed by All Users: " total/1024/1024/1024 "GB"}';
echo ""
Actual data Result:
$ sh Ftpcal.sh
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
Sample data:
-rw-r--r-- 1 34130 14063436 Aug 15 2002 /current/focus-del/files/from_fix.v.gz
-rw-r--r-- 1 34130 14060876 Jul 12 2007 /current/focus-del/files/from1_fix.v.gz
-rw-r--r-- 1 34130 58668461 Feb 23 2006 /current/focus-del/files/from_1.tar.gz
-rw-r--r-- 1 34130 14069343 Aug 7 20017 /current/focus-del/files/from_tm_fix.v.gz
-rw-r--r-- 1 34130 38179000 Dec 7 20016 /current/focus-del/files/from_tm.gds.gz
-rw-r--r-- 1 34130 15157902 Nov 22 20015 /current/focus-del/files/from_for.tar.gz
-rw-r--r-- 1 34130 97986560 Nov 4 20015 /current/focus-del/files/from_layout.tar
Sample Result:
$ sh Ftp_cal.sh
User 34130 Total Space consumed: 0.0808321GB
==============================================================
Total Space consumed by All Users: 0.0808321GB
I'm okay with any better approach as a review process to make it more robust.
Thanks.