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!

Adding data from different lines 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I have the following sample file:

VE_ORINOCO_FULL 100 99.9921875
VE_ORINOCO_FULL 2264 11.609375
VE_SINCOR_3_2004 4000 967.296875
VE_SINCOR_SED_3D 740 273.039063
XX_AO_TEST 110 42.8203125
XX_BODATALOAD 110 37.1640625
XX_CUTOFF_LD 126 2.3671875
XX_CUTOFF_TEST 110 90.296875
XX_FLOUNDER 100 87.1640625
XX_FLOUNDER 300 185.320313
XX_FLOUNDER_TRAIN 150 123.25

I would like to make a script with the name in the first column as argument then to search for all the lines that match that name EXACTLY and add the value of the second column.

For instance: script XX_FLOUNDER < inputfile would return in the above case: 400.

Many thanks,

 
awk -v v=$1 '$1==v{t+=$2}END{print 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
 
Thanks guys but I am not sure this is doing what I want:

I need to add this awk line in a shell script so something like

P="XX_FLOUNDER"

size=`nawk -v n="$P" 'n=="$P" {s+=$2} END{print s}' /tmp/owspace.lst`
echo $size

but I get no output!



 
Try

size=`nawk -v n="$P" 'n==$1{s+=$2} END{print s}' /tmp/owspace.lst`
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top