Sponsored Content
Top Forums Shell Programming and Scripting How to find sum of any 'n' number of values from file matching target value? Post 302843948 by Chubler_XL on Thursday 15th of August 2013 07:11:26 PM
Old 08-15-2013
How about:

Code:
sort -n infile | awk -vn=4 -vt=209.35 '
function check(vals, s, need, depth)
{
    for (;s<=X; s++) {
       if(depth && F[s]<need) check(vals ORS F[s], s+1, need-F[s], depth-1)
       if(!depth&&F[s]==need) printf vals ORS F[s] ORS
       if(F[s]>=need) return
    }
}
{F[++X]=$0}
END { check("", 1, t, n-1) }'

8.10
25.5
75.5
100.25


Last edited by Chubler_XL; 08-15-2013 at 08:25 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Write a shell program to find the sum of alternate digits in a given 5-digit number

Hi Can any one please post the answer for the above program.................. (4 Replies)
Discussion started by: banta
4 Replies

2. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

3. Shell Programming and Scripting

How do I find the sum of values from two arrays?

Hi I have redc containing the values 3, 6, 2, 8, and 1. I have work containing the values 8, 2, 11, 7, and 9. Is there a way to find the sum of redc and work? I need to compare the sum of those two arrays to something else, so is it okay to put that into my END? TY! (4 Replies)
Discussion started by: razrnaga
4 Replies

4. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

5. Shell Programming and Scripting

Taking sum up all values inside the file

Hi, Taking sum up all values inside the file by using the below command: paste -sd+ filenmae | bc Getting some error like "0705-001: building space exceeded on line1 stdin" The original data looks like SPACE SPACE SPACE 0.123 JOBNAME1 SPACE SPACE 20.325 JOBNAME2 SPACE SPACE... (2 Replies)
Discussion started by: NareshN
2 Replies

6. Shell Programming and Scripting

Get the sum of values in between begin and end in the file

Hi All, test file Begin Script Run at Thu Mar 14 09:24:16 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: test_20130313.dat count: 63574 loaded: xx pre-merge: xx post-merge: xx timestamp: Thu Mar 14 09:30:42 PDT 2013 tst_accounts: ws zip: WS_out_20130313.tar.gz dat: s_20130313.dat... (6 Replies)
Discussion started by: bmk
6 Replies

7. UNIX for Dummies Questions & Answers

Grep to find matching patern and return unique values

Request: grep to find given matching patern and return unique values, eliminate the duplicate values I have to retrieve the unique folder on the below file contents like; /app/oracle/build_lib/pkg320.0_20120927 /app/oracle/build_lib/pkg320.0_20121004_prof... (5 Replies)
Discussion started by: Siva SQL
5 Replies

8. Shell Programming and Scripting

Sum column values matching other field

this is part of a KT i am going thru. i am writing a script in bash shell, linux where i have 2 columns where 1st signifies the nth hour like 00, 01, 02...23 and 2nd the file size. sample data attached. Desired output is 3 columns which will give the nth hour, number of entries in nth hour and... (3 Replies)
Discussion started by: alpha_1
3 Replies

9. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. Shell Programming and Scripting

How to sum values on specific line number?

I have a file that looks like this: HP ColorPlotter Z-6100 ACMARTIN IP 192.168.x.x "VIRTUAL HP ( C9468A ) PART 1 of 2 (REAL CARTRIDGE 1)" "VIRTUAL HP ( C9468A ) PART 2 of 2 (REAL CARTRIDGE 1)" 181 181 "VIRTUAL HP ( C9471A ) PART 1 of 2 (REAL CARTRIDGE 2)" "VIRTUAL HP ( C9471A ) PART 2... (4 Replies)
Discussion started by: SysAdminRialto
4 Replies
ldap_sort(3LDAP)					      LDAP Library Functions						  ldap_sort(3LDAP)

NAME
ldap_sort, ldap_sort_entries, ldap_sort_values, ldap_sort_strcasecmp - LDAP entry sorting functions SYNOPSIS
cc[ flag... ] file... -lldap[ library... ] #include <lber.h> #include <ldap.h> ldap_sort_entries(LDAP *ld, LDAPMessage **chain, char *attr, int (*cmp)()); ldap_sort_values(LDAP *ld, char **vals, int (*cmp)()); ldap_sort_strcasecmp(char *a, char *b); DESCRIPTION
These functions are used to sort lists of entries and values retrieved from an LDAP server. ldap_sort_entries() is used to sort a chain of entries retrieved from an LDAP search call either by DN or by some arbitrary attribute in the entries. It takes ld, the LDAP structure, which is only used for error reporting, chain, the list of entries as returned by ldap_search_s(3LDAP) or ldap_result(3LDAP). attr is the attribute to use as a key in the sort or NULL to sort by DN, and cmp is the comparison function to use when comparing values (or individ- ual DN components if sorting by DN). In this case, cmp should be a function taking two single values of the attr to sort by, and return- ing a value less than zero, equal to zero, or greater than zero, depending on whether the first argument is less than, equal to, or greater than the second argument. The convention is the same as used by qsort(3C), which is called to do the actual sorting. ldap_sort_values() is used to sort an array of values from an entry, as returned by ldap_get_values(3LDAP). It takes the LDAP connection structure ld, the array of values to sort vals, and cmp, the comparison function to use during the sort. Note that cmp will be passed a pointer to each element in the vals array, so if you pass the normal char ** for this parameter, cmp should take two char **'s as arguments (that is, you cannot pass strcasecmp or its friends for cmp). You can, however, pass the function ldap_sort_strcasecmp() for this pur- pose. For example: LDAP *ld; LDAPMessage *res; /* ... call to ldap_search_s(), fill in res, retrieve sn attr ... */ /* now sort the entries on surname attribute */ if ( ldap_sort_entries( ld, &res, "sn", ldap_sort_strcasecmp ) != 0 ) ldap_perror( ld, "ldap_sort_entries" ); ATTRIBUTES
See attributes(5) for a description of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |Availability |SUNWcsl (32-bit) | | |SUNWcslx (64-bit) | |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
ldap(3LDAP), ldap_search(3LDAP), ldap_result(3LDAP), qsort(3C), attributes(5) NOTES
The ldap_sort_entries() function applies the comparison function to each value of the attribute in the array as returned by a call to ldap_get_values(3LDAP), until a mismatch is found. This works fine for single-valued attributes, but may produce unexpected results for multi-valued attributes. When sorting by DN, the comparison function is applied to an exploded version of the DN, without types. The return values for all of these functions are declared in the <ldap.h> header file. Some functions may allocate memory which must be freed by the calling application. SunOS 5.11 27 Jan 2002 ldap_sort(3LDAP)
All times are GMT -4. The time now is 02:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy