Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix or perl matching

Status
Not open for further replies.

uguess

Technical User
Nov 5, 2004
40
CA
My question is on following file: (either use korn shell or perl to solve)

51 19UTEX B
1008 19UTEX N
1480 19UTEX X
6942 19UTEX Y


How can I achieve the following. I want to get the total of left most field with numbers. Then subtract count of right most filed that either says "B" or "N".

Forexample:

Total of left most field is => 9481
Count of right most field that has letter "B" is => 51
Count of right most field that has letter "N" is => 1008

Hensce new number = 9481 - (subtract) (51+1008) ==> 8422

SInce i have to get following number manually, Is this possible with the help of perl or korn script. the file will always look the same.

Thanks
 
In korn-shell you may try something like this:
NewNumber=$(awk '$3!="B" && $3!="N"{t+=$1}END{printf "%d",t}' /path/to/input)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Though I will add that I like the terseness of PHVs solution, here is the same in a script with ksh.

Code:
#!/bin/ksh

p=0
while read a b c 
do
        if [[ $c = B || $c = N ]] then
                continue
        else
                ((p+=$a))
        fi
done < t
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top