Help with awk percentage calculation from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk percentage calculation from a file
# 1  
Old 12-27-2016
RedHat Help with awk percentage calculation from a file

i have a file say test with the below mentioned details

Code:
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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-27-2016 at 08:27 AM.. Reason: Added CODE tags.
# 2  
Old 12-27-2016
Hello venkitesh,

Welcome to forums, could you please try following and let me know if this helps.
Code:
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.
Code:
/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.
Code:
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

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-28-2016 at 02:25 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-27-2016
Hi Ravinder,

Thanks for the script.


is it possible to print the output like this.

Code:
test1(removing the first slash)   500gb 100gb 98%

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 01-02-2017 at 04:04 AM..
# 4  
Old 12-27-2016
Quote:
Originally Posted by venkitesh
Hi Ravinder,
Thanks for the script.
is it possible to print the output like this.
test1(removing the first slash) 500gb 100gb 98%
Hello venkitesh,

Could you please try following and let me know if this helps.
Code:
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){sub(/.*\//,X,$1);print $1,TOT $2,USED $3,VAL"%"};VAL=""}'  Input_file

Output will be as follows.
Code:
test1 500.100.1GB 11212.0GB 22.3955%

Adding a non-one liner form of solution successfully too now.
Code:
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)                             {
                                                        sub(/.*\//,X,$1);
                                                        print $1,TOT $2,USED $3,VAL"%"
                                               };
        VAL=""
     }
    '    Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-27-2016 at 07:27 AM..
# 5  
Old 12-27-2016
HI ravinder,

is there any way we can keep tb and gb as it is instead of converting all to gb's

as some are coming like this 50.010.01GB.

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 01-02-2017 at 04:02 AM..
# 6  
Old 12-27-2016
Hello venkitesh,

No, It is not touching the values from Input_file. It is showing TB/GB as per Input_file only, so let's say we have following Input_file.
Code:
cat Input_file
/test/test1 500.1GB 112.0GB
/test/test2 3.2TB 1.3TB
/test/test3 3TB 100GB

Then following is the code.
Code:
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)                             {
                                                        sub(/.*\//,X,$1);
                                                        print $1 FS TOT $2 FS USED $3 FS VAL"%"
                                               };
        VAL=""
     }
    '   Input_file

Output will be as follows.
Code:
test1 500.1GB 112GB 22.3955%
test2 3.2TB 1.3TB 40.625%

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-27-2016 at 07:26 AM..
# 7  
Old 12-27-2016
HI ravinder,

please find the output what i am getting

original file content

Code:
/test/test1/test1       200GB    192.7GB

after applying awk command to this file output comes like this

Code:
test1        20000GB  192.792.7GB  96.35%

if i change the same content of input file as above

it comes like this

Code:
test1  500.100.1GB  11212.0GB  22.3955%


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-27-2016 at 08:41 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Shell /awk script for Percentage

having two columns, A and B.. i need to add another column C in a file and calculate the percentage based on the column A and B. (COLUMN B/ COLUMN A *100) . "|" is delimiter separating the A and B.. need C column with the percentage value. Thanks for your help 100|50 |50% ... (6 Replies)
Discussion started by: kartikirans
6 Replies

2. Shell Programming and Scripting

Percentage calculation

Hi, I have a text file in below format. I trying to find a solution for finding percentage used for each of the NAMEs. Directory ALLOCATED USED NAME1 93MB 93KB NAME2 25G 62K NAME3 14G 873M NAME4 25G 62K NAME5 20G... (10 Replies)
Discussion started by: ctrld
10 Replies

3. Shell Programming and Scripting

find percentage - awk

Please help me with this ... Input file /vol/test1 10G /vol/test2 1G /vol/test3 200G /vol/test4 3G Output File /vol/test1 10G - - 9G - /vol/test2 1024M - - 921M - /vol/test3 200G - - 180G - /vol/test4 3072M - - 2764M - Basically if Column 2 ( which is... (6 Replies)
Discussion started by: greycells
6 Replies

4. Shell Programming and Scripting

Percentage calculation

i am trying to get percentage : but not able to do it: i tried : x=1 y=2 z=`expr $x/$y*100` it is not giving me result can u pls help on this (4 Replies)
Discussion started by: Aditya.Gurgaon
4 Replies

5. Shell Programming and Scripting

Percentage Calculation in Decimal

Hi, I have two variable and I need to calculate the percentage of them. Example: (b-a)*100/b How can I do it? I need to do it till 2 decimal point. (16 Replies)
Discussion started by: Anupam_Halder
16 Replies

6. Shell Programming and Scripting

awk/sed percentage calculation

Hi all i have a text file with columns delimited with , 2010-08-18,10,24,.09751,39,7,14872,26732 . . . i would to add a extra column in the end with percentage calculation of columns 5 and 8 ie (39/26732)*100 so the output must look like ... (6 Replies)
Discussion started by: posner
6 Replies

7. Shell Programming and Scripting

File Size calculation with AWK

Hello Friends, Im calculating file sizes with below AWK script. I do this before some spesific files are transferred. I run the script it works but after several running it stuck with a limit of 2147483647 (2 Gbytes -1 byte) and cant exceed this. Something is wrong and I can't proceed, would... (1 Reply)
Discussion started by: EAGL€
1 Replies

8. Shell Programming and Scripting

awk script to count percentage from log file

Hi, I have a log like this : actually i want to get the log like this : where % can get from : 100 * pmTotNoRrcConnectReqSucc / pmTotNoRrcConnectReq Thanks in advance.. :) (8 Replies)
Discussion started by: justbow
8 Replies

9. Shell Programming and Scripting

Need an AWK script to calculate the percentage

Hi I need a awk script to calculate percentage. I have to pass the pararmeters in to the awk script and calculate the percentage. Sum = 50 passed = 43 failed = 7 I need to pass these value in to the awk script and calculate the percentage. Please advice me. (8 Replies)
Discussion started by: bobprabhu
8 Replies

10. Shell Programming and Scripting

awk percentage

how would you calculate percentage by per line? Given a column of 16 lines, grab each line and divide it by the sum of the entire column and multiply by 100? thanks ... (8 Replies)
Discussion started by: rockiefx
8 Replies
Login or Register to Ask a Question