i have a file say test with the below mentioned details
Folder Name Total space Space used
/test/test1 500.1GB 112.0 GB
/test/test2 3.2 TB 5TB
/test/test3 3TB 100GB
i need to calculate percentage of each row based on total space and space used and copy the contents of particular rows in a new file (test1) which has crossed more than 90%(here some are in gb and some in TB). Kindly help
Welcome to forums, could you please try following and let me know if this helps.
awk '{TOT=$2+0;gsub(/[[:digit:]]|\./,X,$2);USED=$3+0;gsub(/[[:digit:]]|\./,X,$3);if(($2 ~ /GB/ || $2 ~ /TB/)&& TOT>=USED){VAL=USED/TOT * 100};if($2 ~ /TB/ && $3 ~ /GB/ && TOT>=USED){VAL=USED/(TOT * 1024) * 100};if(VAL>20){print $1" filesystem has more disk space used than threshold."};VAL=""}' Input_file
Output will be as follows.
/test/test1 filesystem has more disk space used than threshold.
So in above code I have put 20 as threshold, you could set it as per your requirement. I hope this helps(It only handles filesystem capacity in either TBs and GBs).
EDIT: Adding a non-one liner form of solution now too.
awk '{
TOT=$2+0;
gsub(/[[:digit:]]|\./,X,$2);
USED=$3+0;
gsub(/[[:digit:]]|\./,X,$3);
if(($2 ~ /GB/ || $2 ~ /TB/) && TOT>=USED){
VAL=USED/TOT * 100
};
if($2 ~ /TB/ && $3 ~ /GB/ && TOT>=USED){
VAL=USED/(TOT * 1024) * 100
};
if(VAL>20){
print $1" filesystem has more disk space used than threshold."
};
VAL=""
}
' Input_file
I had assumed that there are no spaces between digit values of a filesystem and GB/TB strings as I saw discrepancy in your very first Input_file, so if you do not have space between filesystem's values and TB/GB strings this command should run then. Let me know if you have any queries on same.
If you look at the links at the bottom left of this page (under "More UNIX and Linux Forum Topics You Might Find Helpful"), you might find very useful hints. For instance, this post applied to your problem in post#1 immediately yields
Folder Name Total space Space used percent
/test/test1 500.1GB 112.0GB 22.40%
/test/test2 3.2TB 5TB 156.25%
/test/test3 3TB 100GB 3.26%
It requires that numbers and units NOT be separated by spaces, though. 5TB used on a 3.2TB disk is quite ambitious, isn't it?
Searching these forums may yield further proposals and hints...