# Shell scripting

**URL:** <https://community.unix.com/t/shell-scripting/320481>\
**Category:** Shell Programming and Scripting\
**Created:** [November 28, 2012, 1:31am UTC](https://community.unix.com/t/shell-scripting/320481 "2012-11-28T01:31:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pradeep\_rawat](https://community.unix.com/letter_avatar/pradeep_rawat/32/6_449c533f586939b6e5f246632a46421f.png) [@pradeep\_rawat](https://community.unix.com/u/pradeep_rawat)\
**Post date:** [November 28, 2012, 1:31am UTC](https://community.unix.com/t/shell-scripting/320481/1 "2012-11-28T01:31:03Z")

</div>

```nohighlight
id,name,id_2, success/failure
44,RAJ, 33, SUCCESS
40,JEY, 44,FAILURE
44,RAJ, 33, FAILURE
40,JEY, 44,FAILURE
40,JEY, 44,FAILURE
44,RAJ, 33,FAILURE

```

THIS IS MY FILE. I WANT OUTPUT AS.

```nohighlight
id,name,id_2,success/failure,TOTAL
44,RAJ,33,SUCCESS=1/FAILURE=2, TOTAL=3
40,JEY,44,SUCCESS=0/FAILURE=3, TOTAL=3

```

CAN ANYONE HELP ME WITH DIS PLEASE.THANKS

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_449c533f586939b6e5f246632a46421f.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [November 28, 2012, 1:54am UTC](https://community.unix.com/t/shell-scripting/320481/2 "2012-11-28T01:54:42Z")

</div>

Using Scrutinizer's solution from previous post.

```nohighlight
$ cat file
id,name,id_2, success/failure
44,RAJ, 33, SUCCESS
40,JEY, 44,FAILURE
44,RAJ, 33, FAILURE
40,JEY, 44,FAILURE
40,JEY, 44,FAILURE
44,RAJ, 33,FAILURE

$ awk -F, 'NR==1{print}
NR>1{gsub(" ","",$0);A[$1","$2","$3]; B[$4] ; C[$1","$2","$3,$4]++}END{for(i in A){p=i; for(j in B){k+=C[i,j];p=p FS j "=" C[i,j]+0;} print p ",Total=" k;k=0}}' file

id,name,id_2, success/failure
40,JEY,44,FAILURE=3,SUCCESS=0,Total=3
44,RAJ,33,FAILURE=2,SUCCESS=1,Total=3

```

---

<div class="post-metadata">

**Author:** ![pradeep\_rawat](https://community.unix.com/letter_avatar/pradeep_rawat/32/6_449c533f586939b6e5f246632a46421f.png) [@pradeep\_rawat](https://community.unix.com/u/pradeep_rawat)\
**Post date:** [November 28, 2012, 2:09am UTC](https://community.unix.com/t/shell-scripting/320481/3 "2012-11-28T02:09:16Z")

</div>

```nohighlight
40,jJEY,44,FAILURE,=3,Total=3
40,JEY,44,SUCCESS,=0,Total=0
44,RAJ,33,FAILURE,=2,Total=2
44,RAJ,33,SUCCESS,=1,Total=1

```

@PAMU....I M GETTING OUTPUT THIS WAY

---------- Post updated at 02:09 AM ---------- Previous update was at 02:05 AM ----------

sorry sorry pamu ur script is correct i only did mistak thanx alot for saving my ass 😛 really appreciate your work 🙂

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_449c533f586939b6e5f246632a46421f.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [November 28, 2012, 2:18am UTC](https://community.unix.com/t/shell-scripting/320481/4 "2012-11-28T02:18:15Z")

</div>

I have modified the script to get your desired output with "/" in between.🙂

```nohighlight
$ cat file
id,name,id_2, success/failure
44,RAJ, 33, SUCCESS
40,JEY, 44,FAILURE
44,RAJ, 33, FAILURE
40,JEY, 44,FAILURE
40,JEY, 44,FAILURE
44,RAJ, 33,FAILURE

$ awk -F, 'NR==1{print}
NR>1{gsub(" ","",$0);A[$1","$2","$3]; B[$4] ; C[$1","$2","$3,$4]++}END{
for(i in A){p=i; for(j in B){k+=C[i,j];
T=T?T "/" j "=" C[i,j]+0:j "=" C[i,j]+0;} print p","T ",Total=" k;k=0;T=""}}' file

id,name,id_2, success/failure
40,JEY,44,FAILURE=3/SUCCESS=0,Total=3
44,RAJ,33,FAILURE=2/SUCCESS=1,Total=3

```

---

<div class="post-metadata">

**Author:** ![pradeep\_rawat](https://community.unix.com/letter_avatar/pradeep_rawat/32/6_449c533f586939b6e5f246632a46421f.png) [@pradeep\_rawat](https://community.unix.com/u/pradeep_rawat)\
**Post date:** [November 28, 2012, 2:31am UTC](https://community.unix.com/t/shell-scripting/320481/5 "2012-11-28T02:31:23Z")

</div>

yeah i used Scrutinizer's code for previous one and your in this new one once again hats off 🙂

---

<div class="post-metadata">

**Author:** ![Jotne](https://community.unix.com/user_avatar/community.unix.com/jotne/32/2148_2.png) [@Jotne](https://community.unix.com/u/Jotne)\
**Post date:** [November 28, 2012, 2:40am UTC](https://community.unix.com/t/shell-scripting/320481/6 "2012-11-28T02:40:02Z")

</div>

This post is nearly the same as you posted here

> **[Linux script](https://www.unix.com/shell-programming-and-scripting/208169-linux-script.html)**
>
> can anyone help in this i have a file like pre { overflow:scroll; margin:2px; padding:15px; border:3px inset; margin-right:10px; } Code: 2089,success 2089,failure 2087,failure 2089,success 2087,failur | The UNIX and Linux Forums

I guess you can modify the script there to work for you  
Why starting a new thread?

And do NOT use caps look

---

<div class="post-metadata">

**Author:** ![pradeep\_rawat](https://community.unix.com/letter_avatar/pradeep_rawat/32/6_449c533f586939b6e5f246632a46421f.png) [@pradeep\_rawat](https://community.unix.com/u/pradeep_rawat)\
**Post date:** [November 28, 2012, 2:50am UTC](https://community.unix.com/t/shell-scripting/320481/7 "2012-11-28T02:50:27Z")

</div>

@pamu === i m still not getting TOTAL heading  
@Jotne = ya bro i wont i m new here so sorry for tht

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_449c533f586939b6e5f246632a46421f.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [November 28, 2012, 3:22am UTC](https://community.unix.com/t/shell-scripting/320481/8 "2012-11-28T03:22:40Z")

</div>

> [@pradeep rawat](#):
>
> @pamu === i m still not getting TOTAL heading

Please show us what you have tried.

---

<div class="post-metadata">

**Author:** ![pradeep\_rawat](https://community.unix.com/letter_avatar/pradeep_rawat/32/6_449c533f586939b6e5f246632a46421f.png) [@pradeep\_rawat](https://community.unix.com/u/pradeep_rawat)\
**Post date:** [November 28, 2012, 7:09am UTC](https://community.unix.com/t/shell-scripting/320481/9 "2012-11-28T07:09:31Z")

</div>

i have a newfile now  
its like dis

```nohighlight
id,name,id_2, type,success/failure,number
44,RAJ, 33, a,SUCCESS,30
40,JEY, 44,a,FAILURE,30
44,RAJ, 33, a,FAILURE,30
40,JEY, 44,a,FAILURE,30
40,JEY, 44,a,FAILURE,30
44,RAJ, 33,a,FAILURE,30

```

and i have lil problem now because now i hav to add two more column in output

id,name,id\_2, type, success/failure,total,number,number2(total/3\*3),number3(number2/number \*100)

```nohighlight
40,JEY,44,a,FAILURE=3/SUCCESS=0,Total=3,30,3,10
44,RAJ,33,a,FAILURE=2/SUCCESS=1,Total=3,30,310

```

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/6_449c533f586939b6e5f246632a46421f.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [November 28, 2012, 7:19am UTC](https://community.unix.com/t/shell-scripting/320481/10 "2012-11-28T07:19:51Z")

</div>

Try

```nohighlight
$ cat file
id,name,id_2, type,success/failure,number
44,RAJ, 33, a,SUCCESS,30
40,JEY, 44,a,FAILURE,30
44,RAJ, 33, a,FAILURE,30
40,JEY, 44,a,FAILURE,30
40,JEY, 44,a,FAILURE,30
44,RAJ, 33,a,FAILURE,30

$ awk -F, 'NR==1{print}
NR>1{gsub(" ","",$0);A[$1","$2","$3","$4]=$6; B[$5] ; C[$1","$2","$3","$4,$5]++}END{
for(i in A){p=i; for(j in B){k+=C[i,j];
T=T?T "/" j "=" C[i,j]+0:j "=" C[i,j]+0;};num_1=A;num_3=(k/num_1)*100
print p","T ",Total=" k,num_1,k,num_3;k=0;T=""}}' OFS="," file

id,name,id_2, type,success/failure,number
40,JEY,44,a,FAILURE=3/SUCCESS=0,Total=3,30,3,10
44,RAJ,33,a,FAILURE=2/SUCCESS=1,Total=3,30,3,10

```

And one important thing - don't expect script should run on any input.  
As per your input we will create any script. If input changes there is no chance that script should run for other input also.

pamu

---

<div class="post-metadata">

**Author:** ![pradeep\_rawat](https://community.unix.com/letter_avatar/pradeep_rawat/32/6_449c533f586939b6e5f246632a46421f.png) [@pradeep\_rawat](https://community.unix.com/u/pradeep_rawat)\
**Post date:** [November 29, 2012, 12:10am UTC](https://community.unix.com/t/shell-scripting/320481/11 "2012-11-29T00:10:25Z")

</div>

i have a file like is with a delimiter ","  
id,name,type,cp\_id,status,price  
23,raj,normal,32,success,30  
23,raj,normal,32,success,30  
23,raj,normal,32,failure,30  
24,jey,normal,42,failure,30  
24,jey,normal,42,failure,30  
24,jey,normal,42,success,30

i want my output to consist of four new columns which are (success,failure,number(total\3\*3),number\_2(number/price_100)  
like dis.  
id,name,type,cp\_id,success,failure,total,price,number(total\3\*3),number\_2(number/price_100)  
23,raj,normal,32,2,1,3,30,3,30  
24,jey,normal,42,1,2,3,30,3,0

anyone can show interest to solve it for me cos i m struck cos i m just able to produce till sucess,failure.
