Shell /awk script for Percentage


 
Thread Tools Search this Thread
Operating Systems HP-UX Shell /awk script for Percentage
# 1  
Old 06-22-2018
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

Code:
100|50   |50%          (50)/100*100 )
200|200  |100%


Last edited by kartikirans; 06-22-2018 at 11:19 PM..
# 2  
Old 06-22-2018
Please show us a sample input file, the corresponding output file you are hoping to produce, and whatever awk code you have tried to use to solve this problem on your own.

I don't understand what the 4th and 5th fields in the first line of your desired output are supposed to be (nor why having mismatched parentheses in your output is desirable)???

Are the <space>s at the end of field 2 in your output present in your input file? If not, why are they present in your output file? Is your output field separator just a vertical bar character (|) or is there some reason why field 2 and field 3 in your output are separated by a variable number of <space>s and a vertical bar character? And, why are the 4th and 5th fields separated by varying numbers of <space>s instead of by vertical bar characters?
# 3  
Old 06-23-2018
Thanks Don,

Input having only 2 columns A and B

Code:
A|B
100|50  
200|200

Third column has to be generated with %Percentage ( B/A*100)

Like below -

Code:
A|B|C
100|50|50%
200|200|100%

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments; as required by forum rules.

Last edited by Don Cragun; 06-23-2018 at 11:56 PM.. Reason: Add CODE tags, again.
# 4  
Old 06-23-2018
Hello kartikirans,

Could you please try following and let me know if this helps you.

Code:
awk 'BEGIN{FS=OFS="|"}FNR==1{print $0,"C";next} {sub(/ +$/,"");$(NF+1)=($2/$1)*100} 1'  Input_file

Thanks,
R. Singh
# 5  
Old 06-23-2018
Thanks Singh, command looks good for me. If there are strings in between , can we ignore this and add as NA


Code:
A|B
100|50 
200|200
DDD|DDD

Expected-

Code:
A|B|C
100|50|50%
200|200|100%
DDD|DDD|NA


Last edited by Don Cragun; 06-23-2018 at 11:57 PM.. Reason: Add CODE tags, again.
# 6  
Old 06-24-2018
Maybe something like:
Code:
awk '
BEGIN {	FS = OFS = "|"
}
NR == 1 {
	print $0, "C"
	next
}
{	gsub(/ /, "")
	if($1 $2 ~ /[^0-9]/)
		$3 = "NA"
	else	$3 = $2 / $1 * 100 "%"
}
1' file

will give you what you want? (Note that this assumes that all <space>s in fields 1 and 2 should be deleted, while the code Ravinder suggested only deletes trailing <space>s in those two fields.)
# 7  
Old 06-24-2018
Quote:
Originally Posted by kartikirans
Thanks Singh, command looks good for me. If there are strings in between , can we ignore this and add as NA
Code:
A|B
100|50 
200|200
DDD|DDD

Expected-
Code:
A|B|C
100|50|50%
200|200|100%
DDD|DDD|NA

Hello kartikirans,

Could you please try following and let me know if this helps you.

Code:
awk 'BEGIN{FS=OFS="|"}FNR==1{print $0,"C";next} {sub(/ +$/,"");$(NF+1)=$1~/[0-9/+/||$2~/[0-9]+/?($2/$1)*100"%":"NA"} 1'  Input_file

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Working out percentage in shell script

Hi All, I currently have a shell script which is pulling multiple counters from various sources. Due to the counters being cumulative counters I've got some code to work out the delta from the last reading and current which is working fine. The problem i have now is being able to work out the... (8 Replies)
Discussion started by: mutley2202
8 Replies

2. UNIX for Dummies Questions & Answers

Percentage / Multiplication in Shell Script

Hello, I am trying to compute the percentage in a script as shown below: PerCover=`echo "scale=2 ; 100 \* ($InputCover/$Total)" | bc` However the PerCover value is blank/null. What do I need to do differently? Thanks for your input! ~Guss (1 Reply)
Discussion started by: Gussifinknottle
1 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. UNIX for Dummies Questions & Answers

Need an awk script to calculate the percentage of value field and replace

I have a input file called file.txt with the following content: john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2 andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1 in fourth filed of input file, calulate percentage of each sub filed seperated by ; semicolon and replace value with percentage . i... (11 Replies)
Discussion started by: veeruasu
11 Replies

5. Shell Programming and Scripting

Need an awk script to calculate the percentage of value field and replace

Need an awk script to calculate the percentage of value field and replace I have a input file called file.txt with the following content: john|622.5674603562933|8|br:1;cn:3;fr:1;jp:1;us:2 andy|0.0|12|**:3;br:1;ca:2;de:2;dz:1;fr:2;nl:1 in fourth filed of input file, calulate percentage of each... (1 Reply)
Discussion started by: veeruasu
1 Replies

6. Programming

Shell script to find Percentage?

Hi all, I havea log of data.log bear,10000,white bear,5000,black chicken,2000,white chicken,4000,yellow chicken,3000,black lion,6000,yellow lion,1000,white How can we have shell script to get the percentage of each animals? Thanks. (8 Replies)
Discussion started by: sabercats
8 Replies

7. Shell Programming and Scripting

Script shell, how to calculate percentage?

hello, please can you help me. jj and kk are two numbers which are the result of an sql program. I would like to calculate the ratio jj/kk*100. I have done this: ratio=$((jj/kk * 100)) or ratio=`expr $jj \/ expr $kk) but the result is 0 What can i do? Thanks for help. (3 Replies)
Discussion started by: likeaix
3 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