Register
It is currently Sun Sep 21, 2014 4:09 pm

awk


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Nov 28, 2012 11:38 am   

Joined: Wed Nov 28, 2012 11:18 am
Posts: 2
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.


Top
 Profile  
 PostPosted: Wed Nov 28, 2012 10:39 pm   

Joined: Wed Nov 28, 2012 11:18 am
Posts: 2
is there no one in this site?


Top
 Profile  
 PostPosted: Thu Nov 29, 2012 12:16 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
where do data in new columns come from?

we can't do anything by pure magic.
we need explanations, logic, algorithm...!


Top
 Profile  
 PostPosted: Wed Nov 06, 2013 8:32 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
#!/bin/bash

if ((! $#)) || [[ ! -f $1 ]]; then
    printf >&2 'Usage: ./%s file.csv\n' "${0##*/}"
    exit 1
fi

exec 3<"$1"
IFS= read -ru3 header
header=${header/status,price/success,failure}

while IFS=, read -ru3 id name type cp_id status price; do
   [[ $id ]] || continue

   [[ " ${!ids[*]} " != *" $id "* ]] && succ= fail=
   [[ $status = success ]] && ((++succ)) || ((++fail))

   ids[id]=$name,$type,$cp_id,$succ,$fail,$((succ + fail)),$price
done
exec 3>&-

printf %s "$header"; echo ,total,price,number,number_2

for id in "${!ids[@]}"; do
   cols=${ids[id]}

   price=${cols##*,}
   total=${cols%,$price} total=${total##*,}

   number=$(bc <<< "scale = 2; $total / 3 * 3")
   number_2=$(bc <<< "scale = 2; $number / ($price * 100)")

   printf %s $id; printf ,%s "$cols" $number{,_2}; echo
done

Magic! : -)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP