# Shell /awk script for Percentage

**URL:** <https://community.unix.com/t/shell-awk-script-for-percentage/372867>\
**Category:** HP-UX\
**Created:** [June 22, 2018, 9:12pm UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867 "2018-06-22T21:12:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kartikirans](https://community.unix.com/letter_avatar/kartikirans/32/6_449c533f586939b6e5f246632a46421f.png) [@kartikirans](https://community.unix.com/u/kartikirans)\
**Post date:** [June 22, 2018, 9:12pm UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/1 "2018-06-22T21:12:07Z")

</div>

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

```nohighlight
100|50 |50% (50)/100*100 )
200|200 |100%

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [June 22, 2018, 10:57pm UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/2 "2018-06-22T22:57:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kartikirans](https://community.unix.com/letter_avatar/kartikirans/32/6_449c533f586939b6e5f246632a46421f.png) [@kartikirans](https://community.unix.com/u/kartikirans)\
**Post date:** [June 22, 2018, 11:27pm UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/3 "2018-06-22T23:27:51Z")

</div>

Thanks Don,

Input having only 2 columns A and B

```nohighlight
A|B
100|50  
200|200

```

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

Like below -

```nohighlight
A|B|C
100|50|50%
200|200|100%

```

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [June 23, 2018, 1:49am UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/4 "2018-06-23T01:49:22Z")

</div>

Hello kartikirans,

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

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

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![kartikirans](https://community.unix.com/letter_avatar/kartikirans/32/6_449c533f586939b6e5f246632a46421f.png) [@kartikirans](https://community.unix.com/u/kartikirans)\
**Post date:** [June 23, 2018, 8:59pm UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/5 "2018-06-23T20:59:09Z")

</div>

Thanks Singh, command looks good for me. If there are strings in between , can we ignore this and add as NA

```nohighlight
A|B
100|50 
200|200
DDD|DDD

```

Expected-

```nohighlight
A|B|C
100|50|50%
200|200|100%
DDD|DDD|NA

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [June 23, 2018, 11:20pm UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/6 "2018-06-23T23:20:50Z")

</div>

Maybe something like:

```nohighlight
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.)

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [June 24, 2018, 12:18am UTC](https://community.unix.com/t/shell-awk-script-for-percentage/372867/7 "2018-06-24T00:18:06Z")

</div>

> [@kartikirans](#):
>
> Thanks Singh, command looks good for me. If there are strings in between , can we ignore this and add as NA
> 
> ```plaintext
> A|B
> 100|50 
> 200|200
> DDD|DDD
> 
> ```
> 
> Expected-
> 
> ```plaintext
> 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.

```plaintext
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
