Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal Post 302930044 by ongoto on Monday 29th of December 2014 08:43:42 PM
Old 12-29-2014
Another bash attempt...
Code:
#!/bin/bash

hist=./hist
data=./abc.data
sort -k 2 -u $data -o $hist

while read a b
do
    a=0
    while read aa bb
    do
        if [[ "$bb" == $b ]]; then
            a=$(( $a + $aa ))
        fi
    done < $data
    printf "%s %s\n" $a "$b"
done < $hist
rm $hist

# output
# --------------------
# 17690 0 subscriberCreate
# 32 0 subscriberPaymentMethodChange
# 3220 0 subscriberProfileUpdate
# 15546 0 subscriberStatusChange
# 25 4020100 subscriberProfileUpdate
# 2 4020129 subscriberStatusChange
# 2 4020307 subscriberCreate


Last edited by ongoto; 12-29-2014 at 10:00 PM..
This User Gave Thanks to ongoto For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies

2. Shell Programming and Scripting

sum multiple columns based on column value

i have a file - it will be in sorted order on column 1 abc 0 1 abc 2 3 abc 3 5 def 1 7 def 0 1 -------- i'd like (awk maybe?) to get the results (any ideas)??? abc 5 9 def 1 8 (2 Replies)
Discussion started by: jjoe
2 Replies

3. Shell Programming and Scripting

grep data on 2nd line and 3rd column

How do I grep/check the on-hand value on the second line of show_prod script below? In this case it's a "3". So if it's > 0, then run_this, otherwise, quit. > ./show_prod Product Status Onhand Price shoe OK 3 1.1 (6 Replies)
Discussion started by: joker_789us
6 Replies

4. Shell Programming and Scripting

split on the basis of 2nd and 3rd column

file A aa 22 48 ab 22 48 tcf 50 76 gf 50 76 h 89 100 yh 89 100 how can we split the file on the basis of common 2 and third column output like file A-1 aa 22 48 ab 22 48 file A-2 cf 50 76 gf 50 76 (3 Replies)
Discussion started by: cdfd123
3 Replies

5. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

6. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

7. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

8. Shell Programming and Scripting

Sum of Columns Base on First Column

Input :- Hd1;Hd2:hd3;Hd4;Hd5 X;1;2;3;4 Y;2;3;5;6 Z;3;5;6;7 X;10;11;24;16 Y;11;23;21;1 Z;10;13;14;15 X;0;1;2;0 K;0;0;0;0 K;0;0;0;0 I want Sum Data base on first column; Hd1;Hd2:hd3;Hd4;Hd5 X;11;14;29;20 Y;12;26;26;7 Z;13;18;20;22 K;0;0;0;0 (4 Replies)
Discussion started by: pareshkp
4 Replies

9. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

10. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies
Basic(3pm)						User Contributed Perl Documentation						Basic(3pm)

NAME
PDL::Basic -- Basic utility functions for PDL DESCRIPTION
This module contains basic utility functions for creating and manipulating piddles. Most of these functions are simplified interfaces to the more flexible functions in the modules PDL::Primitive and PDL::Slices. SYNOPSIS
use PDL::Basic; FUNCTIONS
xvals Fills a piddle with X index values. Uses similar specifications to zeroes and new_from_specification. CAVEAT: If you use the single argument piddle form (top row in the usage table) the output will have the same type as the input; this may give surprising results if, e.g., you have a byte array with a dimension of size greater than 256. To force a type, use the third form. $x = xvals($somearray); $x = xvals([OPTIONAL TYPE],$nx,$ny,$nz...); $x = xvals([OPTIONAL TYPE], $somarray->dims); etc. see zeroes. pdl> print xvals zeroes(5,10) [ [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] [0 1 2 3 4] ] yvals Fills a piddle with Y index values. See the CAVEAT for xvals. $x = yvals($somearray); yvals(inplace($somearray)); $x = yvals([OPTIONAL TYPE],$nx,$ny,$nz...); etc. see zeroes. pdl> print yvals zeroes(5,10) [ [0 0 0 0 0] [1 1 1 1 1] [2 2 2 2 2] [3 3 3 3 3] [4 4 4 4 4] [5 5 5 5 5] [6 6 6 6 6] [7 7 7 7 7] [8 8 8 8 8] [9 9 9 9 9] ] zvals Fills a piddle with Z index values. See the CAVEAT for xvals. $x = zvals($somearray); zvals(inplace($somearray)); $x = zvals([OPTIONAL TYPE],$nx,$ny,$nz...); etc. see zeroes. pdl> print zvals zeroes(3,4,2) [ [ [0 0 0] [0 0 0] [0 0 0] [0 0 0] ] [ [1 1 1] [1 1 1] [1 1 1] [1 1 1] ] ] xlinvals X axis values between endpoints (see xvals). $a = zeroes(100,100); $x = $a->xlinvals(0.5,1.5); $y = $a->ylinvals(-2,-1); # calculate Z for X between 0.5 and 1.5 and # Y between -2 and -1. $z = f($x,$y); "xlinvals", "ylinvals" and "zlinvals" return a piddle with the same shape as their first argument and linearly scaled values between the two other arguments along the given axis. ylinvals Y axis values between endpoints (see yvals). See xlinvals for more information. zlinvals Z axis values between endpoints (see zvals). See xlinvals for more information. xlogvals X axis values logarithmicly spaced between endpoints (see xvals). $a = zeroes(100,100); $x = $a->xlogvals(1e-6,1e-3); $y = $a->ylinvals(1e-4,1e3); # calculate Z for X between 1e-6 and 1e-3 and # Y between 1e-4 and 1e3. $z = f($x,$y); "xlogvals", "ylogvals" and "zlogvals" return a piddle with the same shape as their first argument and logarithmicly scaled values between the two other arguments along the given axis. ylogvals Y axis values logarithmicly spaced between endpoints (see yvals). See xlogvals for more information. zlogvals Z axis values logarithmicly spaced between endpoints (see zvals). See xlogvals for more information. allaxisvals Synonym for ndcoords - enumerates all coordinates in a PDL or dim list, adding an extra dim on the front to accomodate the vector coordinate index (the form expected by indexND, range, and interpND). See ndcoords for more detail. $indices = allaxisvals($pdl); $indices = allaxisvals(@dimlist); $indices = allaxisvals($type,@dimlist); ndcoords Enumerate pixel coordinates for an N-D piddle Returns an enumerated list of coordinates suitable for use in indexND or range: you feed in a dimension list and get out a piddle whose 0th dimension runs over dimension index and whose 1st through Nth dimensions are the dimensions given in the input. If you feed in a piddle instead of a perl list, then the dimension list is used, as in xvals etc. Unlike xvals etc., if you supply a piddle input, you get out a piddle of the default piddle type: double. This causes less surprises than the previous default of keeping the data type of the input piddle since that rarely made sense in most usages. $indices = ndcoords($pdl); $indices = ndcoords(@dimlist); $indices = ndcoords($type,@dimlist); pdl> print ndcoords(2,3) [ [ [0 0] [1 0] ] [ [0 1] [1 1] ] [ [0 2] [1 2] ] ] pdl> $a = zeroes(byte,2,3); # $a is a 2x3 byte piddle pdl> $b = ndcoords($a); # $b inherits $a's type pdl> $c = ndcoords(long,$a->dims); # $c is a long piddle, same dims as $b pdl> help $b; This variable is Byte D [2,2,3] P 0.01Kb pdl> help $c; This variable is Long D [2,2,3] P 0.05Kb hist Create histogram of a piddle $hist = hist($data); ($xvals,$hist) = hist($data); or $hist = hist($data,$min,$max,$step); ($xvals,$hist) = hist($data,[$min,$max,$step]); If "hist" is run in list context, $xvals gives the computed bin centres A nice idiom (with PDL::Graphics::PGPLOT) is bin hist $data; # Plot histogram pdl> p $y [13 10 13 10 9 13 9 12 11 10 10 13 7 6 8 10 11 7 12 9 11 11 12 6 12 7] pdl> $h = hist $y,0,20,1; # hist with step 1, min 0 and 20 bins pdl> p $h [0 0 0 0 0 0 2 3 1 3 5 4 4 4 0 0 0 0 0 0] whist Create a weighted histogram of a piddle $hist = whist($data, $wt, [$min,$max,$step]); ($xvals,$hist) = whist($data, $wt, [$min,$max,$step]); If requested, $xvals gives the computed bin centres. $data and $wt should have the same dimensionality and extents. A nice idiom (with PDL::Graphics::PGPLOT) is bin whist $data, $wt; # Plot histogram pdl> p $y [13 10 13 10 9 13 9 12 11 10 10 13 7 6 8 10 11 7 12 9 11 11 12 6 12 7] pdl> $wt = grandom($y->nelem) pdl> $h = whist $y, $wt, 0, 20, 1 # hist with step 1, min 0 and 20 bins pdl> p $h [0 0 0 0 0 0 -0.49552342 1.7987439 0.39450696 4.0073722 -2.6255299 -2.5084501 2.6458365 4.1671676 0 0 0 0 0 0] sequence Create array filled with a sequence of values $a = sequence($b); $a = sequence [OPTIONAL TYPE], @dims; etc. see zeroes. pdl> p sequence(10) [0 1 2 3 4 5 6 7 8 9] pdl> p sequence(3,4) [ [ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11] ] rvals Fills a piddle with radial distance values from some centre. $r = rvals $piddle,{OPTIONS}; $r = rvals [OPTIONAL TYPE],$nx,$ny,...{OPTIONS}; Options: Centre => [$x,$y,$z...] # Specify centre Center => [$x,$y.$z...] # synonym. Squared => 1 # return distance squared (i.e., don't take the square root) pdl> print rvals long,7,7,{Centre=>[2,2]} [ [2 2 2 2 2 3 4] [2 1 1 1 2 3 4] [2 1 0 1 2 3 4] [2 1 1 1 2 3 4] [2 2 2 2 2 3 4] [3 3 3 3 3 4 5] [4 4 4 4 4 5 5] ] If "Center" is not specified, the midpoint for a given dimension of size "N" is given by " int(N/2) " so that the midpoint always falls on an exact pixel point in the data. For dimensions of even size, that means the midpoint is shifted by 1/2 pixel from the true center of that dimension. Also note that the calculation for "rvals" for integer values does not promote the datatype so you will have wraparound when the value calculated for " r**2 " is greater than the datatype can hold. If you need exact values, be sure to use large integer or floating point datatypes. For a more general metric, one can define, e.g., sub distance { my ($a,$centre,$f) = @_; my ($r) = $a->allaxisvals-$centre; $f->($r); } sub l1 { sumover(abs($_[0])); } sub euclid { use PDL::Math 'pow'; pow(sumover(pow($_[0],2)),0.5); } sub linfty { maximum(abs($_[0])); } so now distance($a, $centre, &euclid); will emulate rvals, while "&l1" and "&linfty" will generate other well-known norms. axisvals Fills a piddle with index values on Nth dimension $z = axisvals ($piddle, $nth); This is the routine, for which xvals, yvals etc are mere shorthands. "axisvals" can be used to fill along any dimension, using a parameter. See also allaxisvals, which generates all axis values simultaneously in a form useful for range, interpND, indexND, etc. Note the 'from specification' style (see zeroes) is not available here, for obvious reasons. transpose transpose rows and columns. $b = transpose($a); pdl> $a = sequence(3,2) pdl> p $a [ [0 1 2] [3 4 5] ] pdl> p transpose( $a ) [ [0 3] [1 4] [2 5] ] perl v5.14.2 2012-01-02 Basic(3pm)
All times are GMT -4. The time now is 06:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy