awk/sed percentage calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/sed percentage calculation
# 1  
Old 08-20-2010
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

2010-08-18,10,24,.09751,39,7,14872,26732,.14
.
.
.


Thanks
# 2  
Old 08-20-2010
Code:
awk  -F, '{printf "%s,%s",$0,substr($5/$8*100,2,3)}' urfile

2010-08-18,10,24,.09751,39,7,14872,26732,.14

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 08-20-2010
thanks rdcwayx works great
# 4  
Old 08-20-2010
Please be careful, my code is not for Rounding.

If you need rounding (0.145893), it should be 0.15
# 5  
Old 08-20-2010
Quote:
Originally Posted by rdcwayx
Please be careful, my code is not for Rounding.

If you need rounding (0.145893), it should be 0.15

it shouldn't now as percentage is very low .14% and its gonna be same low % for rest of the file .

but it would be better if rounding is done in the script

thanks
# 6  
Old 08-20-2010
Somrthing like this,
Code:
awk  -F, '{printf "%s,%.2f\n",$0,($5/$8*100)}' infile

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 08-21-2010
Code:
# x=$(echo "`sed 's/.*\.[0-9]*,\([^,]*\).*/\1/' infile` * 10000 / `sed 's/.*,\([^,]*$\)/\1/' infile`" | bc | sed 's/^/,./') ; sed "s/$/$x/" infile
2010-08-18,10,24,.09751,39,7,14872,26732,.14

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

Help with awk percentage calculation from a file

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... (9 Replies)
Discussion started by: venkitesh
9 Replies

3. 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

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

awk or sed to determine battery percentage

So, I know this can be done but my awk and sed wizardry is not up to snuff for the job. Basically, I have tons and tons of Macbooks at my work and I am trying to grab the current percentage of the battery power on each client. If I use the system_profiler SPPowerDataType command it prints off... (4 Replies)
Discussion started by: tlarkin
4 Replies

8. Shell Programming and Scripting

Antilog calculation in awk or sed

Dear Friends, Anybody knows how to take antilog of an value in unix. Thanks in advance Vasanth (2 Replies)
Discussion started by: vasanth.vadalur
2 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