Sponsored Content
Top Forums Shell Programming and Scripting How to calculate a sum of certain records? Post 74507 by sickboy on Friday 10th of June 2005 09:23:29 AM
Old 06-10-2005
How to calculate a sum of certain records?

Hi,
i have a file where the records are like this.

vt100 2048 D402 MG0010 0
586 262144 D403 MG0011 1000
486 8192 D404 MG0012 270
386 8192 A423 CC0177 40
586 65536 A424 CC0182 670
486 16384 A423 CC0183 100
486 16384 A425 CC0184 80
65000 4096 B407 EE1027 80

I want firstly,
to count how many times each combination of the first 2 letters from the 4th field exist and print it like this:
3 MG or MG 3
5 CC

I tried this :
Code:
awk -F"\t" '{print $4}' pcs.txt | uniq -c

but I didn't know how to count only the first two letters of the fied.

Secondly,
I want to sum the second field for each differrent entry of the 3rd field and print them. Like this
A423 24576
A425 16384

I tried this :
Code:
awk -F"\t" '{a[$3]=$3; 
for (i = 1; i <= NR; i++){
for (i = 1; i <= NR; i++){
if(a[++i] = $3){
size+=$2}
}
print a[i]" "size
}' pcs.txt

but somewhere I have a mistake and I can't find it and also i am not sure if this is going to give me what I want.

Please help me
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column sum group by uniq records

Dear All, I want to get help for below case. I have a file like this. saman 1 gihan 2 saman 4 ravi 1 ravi 2 so i want to get the result, saman 5 gihan 2 ravi 3 like this. Pls help me. (17 Replies)
Discussion started by: Nayanajith
17 Replies

2. Shell Programming and Scripting

Calculate total sum from a file

The file content is dynamic and using this format: name1 number1 name2 number2 name3 number3 name4 number4 .................... Need a smooth way to calculate the sum of all the numbers in that file (number1 + number2 + number3 + number4........ = total ) (11 Replies)
Discussion started by: TehOne
11 Replies

3. Shell Programming and Scripting

awk-calculate records of a text

I want to make a bash shell script that accepts as argument a file name (cars.txt) and: 1) calculates the total price per year and per model. 2) For each car that it's number starts with TK, I want to print the surname and name of the owner and the total cost for them. NBE3452... (3 Replies)
Discussion started by: Mark_orig
3 Replies

4. Solaris

calculate sum size of files by date (arg list too long)

Hi, I wanted a script to find sum of files for a particular date, below is my script ls -lrt *.req | nawk '$6 == "Aug"' | nawk '$7 == "1"'| awk '{sum = sum + $5} END {print sum}' However, i get the error below /usr/bin/ls: arg list too long How do i fix that. Many thanks before. (2 Replies)
Discussion started by: beginningDBA
2 Replies

5. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

6. Shell Programming and Scripting

Help with calculate total sum of same data problem

Long list of input file: AGDRE1 0.1005449050 AGDRE1 2.1005443435 AGDRE1 1.2005449050 AGDRE1 5.1005487870 AASFV3 50.456304789 AASFV3 2.3659706549 AASFV3 6.3489807860 AASFV3 3.0089890148 RTRTRS 5.6546403546 . . Desired output file: AGDRE1 8.5021829410 AASFV3 62.180245240... (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. Homework & Coursework Questions

Calculate sum of the field

Hi All, I need to calculat the sum of the particular field in text file. And the datatype of the field in file is decimal(18,2). If the file is small, then I am facing any problem. But the file is huge, then the result is converted into exponential format. I tried using various command thr... (0 Replies)
Discussion started by: lathanandhini
0 Replies

8. Shell Programming and Scripting

Sum and calculate number in table

Hello everyone, I have some problem in calculation number using awk. input file format : S1 1 : 0.003 0.031 S2 1 : 0.020 0.095 S3 4 : 0.088 0.012 S4 2 : 0.010 0.090 S5 2 : 0.244 0.066 S6 3 : 0.249 0.751 S7 3 : 0.010 0.990 S8 3 : 0.230... (4 Replies)
Discussion started by: awil
4 Replies

9. Shell Programming and Scripting

Help with calculate the total sum of record in column one

Input file: 101M 10M10D20M1I70M 10M10D39M4I48M 10M10D91M 10M10I13M2I7M1I58M 10M10I15M1D66M Output file: 101M 101 0 0 10M10D20M1I70M 100 1 10 10M10D39M4I48M 97 4 10 10M10D91M 101 0 10 10M10I13M2I7M1I58M 88 13 0 10M10I15M1D66M 91 10 1 I'm interested to count how many total of... (6 Replies)
Discussion started by: perl_beginner
6 Replies

10. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies
sprofil(2)							System Calls Manual							sprofil(2)

NAME
sprofil() - execution time profile for disjointed text spaces SYNOPSIS
DESCRIPTION
controls profiling, by which the system maintains estimates of the amount of time the calling program spends executing at various places in its address space. It differs from its predecessor in that it allows simultaneous profiling of many disjointed regions of memory. profp must point to an ordered array of structures. The structure is defined as: profcnt specifies the number of structures in the profp array. If profcnt is 0, profiling is disabled. If non-NULL, tvp points to a which on return contains the time value corresponding to one clock tick. The only valid flags values are and The flags parameter must be set to one of these values. When profiling is enabled, each thread's program counter is examined each clock tick times per second), and the system searches the profp array for a region that includes Each structure is interpreted as follows: pr_base must point to an area of memory whose length (in bytes) is given by pr_size. This memory must not be modified or freed while profiling is enabled. This memory is treated as an array of 2, 4 or 8-byte elements corresponding to the or flag specified by the flags parameter. pr_size must be a non-zero multiple of the element size. pr_offset is the start address of the region to be profiled. Note that it is always interpreted as a text address and never as the address of a function descriptor. pr_scale is interpreted as an unsigned, fixed-point value with sixteen bits to the right of the binary point. The value falls within a region if (offset=((pr_offset) * pr_scale) / 65536) < pr_size. If a matching region is found, offset is rounded down to a 2, 4 or 8-byte boundary corresponding to the element size specified by the flags parameter. The element at pr_base + offset is incremented by 1, unless that would result in overflow. If the calculation above results in an invalid address, profiling is silently disabled. The profp array must be sorted in ascending order by pr_offset value. The profiling regions must not overlap. If a pr_scale field is set to 0 or 1, the corresponding array entry is ignored. The last entry in the profp array is special. If its pr_offset field is set to 0 and its pr_scale field is set to 2, it is treated as an overflow descriptor. In this case, its pr_size field must be set to the size of one element. If the value did not fall within the range of one of the preceding descriptors, the single element specified by the pr_base field is incremented, unless that would result in over- flow. Each call to disables profiling if it was enabled and then enables profiling of the regions specified by the array entries. Due to variations in instruction length on different architectures, the pr_scale setting results in different entry-to-instruction ratios. The following tables show the number of instructions that map into each element of the array that pr_base points to based on instruction size, pr_scale and flags. On PA-RISC machines, instructions are 4 bytes each. +-------+---------------------------------------+ |Scale | PROF_USHORT PROF_UINT PROF_UINT64 | +-------+---------------------------------------+ | 2 | 16384 32768 65536 | | 4 | 8192 16384 32768 | | 8 | 4096 8192 16384 | | 16 | 2048 4096 8192 | | 32 | 1024 2048 4096 | | 64 | 512 1024 2048 | | 128 | 256 512 1024 | | 256 | 128 256 512 | | 512 | 64 128 256 | | 1024 | 32 64 128 | | 2048 | 16 32 64 | | 4096 | 8 16 32 | | 8192 | 4 8 16 | | 16384 | 2 4 8 | | 32768 | 1 2 4 | | 65536 | 0.5 1 2 | |131072 | 0.25 0.5 1 | +-------+---------------------------------------+ On Itanium-based machines, pc points to a 16-byte bundle containing three instructions. It is not possible to profile individual instruc- tions. +------+---------------------------------------+ |Scale | PROF_USHORT PROF_UINT PROF_UINT64 | +------+---------------------------------------+ | 2 | 4096 8192 16384 | | 4 | 2048 4096 8192 | | 8 | 1024 2048 4096 | | 16 | 512 1024 2048 | | 32 | 256 512 1024 | | 64 | 128 256 512 | | 128 | 64 128 256 | | 256 | 32 64 128 | | 512 | 16 32 64 | | 1024 | 8 16 32 | | 2048 | 4 8 16 | | 4096 | 2 4 8 | | 8192 | 1 2 4 | |16384 | 0.5 1 2 | |32768 | 0.25 0.5 1 | +------+---------------------------------------+ Note that a 1-1 mapping of counters consumes twice as much space as for counters, and should only be used in those cases where you expect a 16-bit counter to overflow. Likewise, a 1-1 mapping of counters consumes twice as much space as for counters, and should only be used in those cases where you expect a 32-bit counter to overflow. The results of specifying a pr_scale that corresponds to an instruction-to-profiling element ratio smaller than 1-1 mapping produces unde- fined behavior. If overlapping text regions are specified, then the pr_base associated with the largest offset less than the value of the program counter gets incremented. If more than one text region has the same offset, the first one is chosen. A text region with the pr_offset of 0 and pr_scale of 0x0002 acts as an overflow bin. Any value not previously matched or outside the regions of text space specified is considered to match this text region and the first counter of the specified pr_base is incremented. If specified, such an overflow bin should always be last in the list of specified regions. Notes Profiling is turned off when an is executed, but remains on in both child and parent processes after a A call to has the same effect as calling with just one member in the profp array with the fields profcnt, tvp, and flags taking on the val- ues of and respectively. RETURN VALUE
On success, returns 0. Otherwise, it returns -1 and sets the global variable to indicate the error. ERRORS
The function fails if: profcnt was less than or equal to 0, or greater than Either profp or tvp points to an illegal address. The flags parameter is not valid or a pr_size field is zero or greater than (2^46 * pr_scale) / 65536 or is not a multiple of the element size or the overflow descriptor is not last in the list of specified regions or is more than one element long or the regions are not sorted in ascending order by pr_offset or the regions overlap. SEE ALSO
prof(1), intro(2), profil(2), times(2), monitor(3C), smonitor(3C). sprofil(2)
All times are GMT -4. The time now is 03:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy