Home Man
Search
Today's Posts
Register

HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

Shell /awk script for Percentage

Tags
hp-ux

Login to Reply

 
Thread Tools Search this Thread
# 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 10: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-22-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:
Shell /awk script for Percentage 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 10: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 10:57 PM.. Reason: Add CODE tags, again.
# 6  
Old 06-23-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 to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Working out percentage in shell script mutley2202 UNIX for Beginners Questions & Answers 8 01-30-2017 04:04 PM
Percentage / Multiplication in Shell Script Gussifinknottle UNIX for Dummies Questions & Answers 1 06-09-2014 11:12 AM
Need an awk script to calculate the percentage of value field and replace veeruasu UNIX for Dummies Questions & Answers 11 05-21-2013 08:51 AM
Need an awk script to calculate the percentage of value field and replace veeruasu Shell Programming and Scripting 1 05-21-2013 06:20 AM
Shell script to find Percentage? sabercats Programming 8 02-17-2012 01:38 PM
Script shell, how to calculate percentage? likeaix Shell Programming and Scripting 3 08-04-2011 10:30 AM
Help with bash script - Need to get CPU usage as a percentage graysky Shell Programming and Scripting 3 11-25-2009 09:23 AM
awk script to count percentage from log file justbow Shell Programming and Scripting 8 05-08-2009 01:17 PM
Need an AWK script to calculate the percentage bobprabhu Shell Programming and Scripting 8 11-07-2008 04:42 AM
how to make percentage of a running script? jimmbp Shell Programming and Scripting 1 01-31-2006 01:41 PM


All times are GMT -4. The time now is 12:16 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password