HP-UX

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

👤 Login to reply

    #1  
Old 4 Weeks Ago
kartikirans kartikirans is offline
Registered User
 
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; 4 Weeks Ago at 10:19 PM..
Sponsored Links
    #2  
Old 4 Weeks Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
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?
Sponsored Links
    #3  
Old 4 Weeks Ago
kartikirans kartikirans is offline
Registered User
 
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; 4 Weeks Ago at 10:56 PM.. Reason: Add CODE tags, again.
    #4  
Old 4 Weeks Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
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
Sponsored Links
    #5  
Old 4 Weeks Ago
kartikirans kartikirans is offline
Registered User
 
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; 4 Weeks Ago at 10:57 PM.. Reason: Add CODE tags, again.
Sponsored Links
    #6  
Old 4 Weeks Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
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.)
Sponsored Links
    #7  
Old 4 Weeks Ago
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Quote:
Originally Posted by kartikirans View Post
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
Sponsored Links
👤 Login to reply

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
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
Need an AWK script to calculate the percentage bobprabhu Shell Programming and Scripting 8 11-07-2008 04:42 AM



All times are GMT -4. The time now is 09:32 AM.

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





Not a Forum Member?
Forgot Password?