10-10-2009
Sum value from selected lines script (awk,perl)
Hello.
I face this (2 side) problem.
Some lines with this structure.
...........
12345678 4
12345989 13
12346356 205
12346644 74
12346819 22
.........
The first field (timestamp) is growing (or at least equal).
1)Sum the second fields if the first_field/500 are equals.
2)Sum the second fields if the difference between first fields is less than 500.
(sliding window)
In the example presented.
1) Becouse 12345678/500 and 12345989/500 both result 24691 sum=4+13
We cannot group the 3rd line so sum=205
And we group the 4th and 5th line so sum=74+22
2) We group the 1st and 2nd line becouse 12345989 - 12345678 < 500
For analogy we group the 2nd and 3th, the 3rd and 4th,
and the 3rd,4th and 5th becouse 12346819 (of the 5th line) - 12346356 (of the 3th line) < 500
Is there any (perl,awk,etc...) way to do it?
Thanks
Paolo
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
How can I extract few lines(like 10 to 15, top 10 and last 10) from a file using perl.
I do it with sed, head and tail in unix scripting. I am new to perl. Appreciate your help. (2 Replies)
Discussion started by: paruthiveeran
2 Replies
2. Shell Programming and Scripting
Hi,
I have a file and I want to sum all the numbers in it.
Example of the file:
0.6714359
-3842.59553830551
I used your forum (https://www.unix.com/shell-programming-scripting/74293-how-get-sum-all-lines-file.html) and found a script, what worked for me:
awk '{a+=$0}END{print a}'... (8 Replies)
Discussion started by: mario8eren
8 Replies
3. Shell Programming and Scripting
Hi Experts,
The question may look very silly by seeing the title, but please have a look at it clearly.
I have a text file where the first 5 columns in each row were supposed to be attributes of a sample(like sample name, number, status etc) and the next 25 columns are parameters on which... (3 Replies)
Discussion started by: ks_reddy
3 Replies
4. Shell Programming and Scripting
Hi,
I have a file with format
a b c d e
1 1 2 2 2
1 2 2 2 3
1 1 1 1 2
1 1 1 1 4
1 1 1 1 6
in column e i want to find all similar fields ( with perl script )and sum it how many are there
for instance in format above.
2 - 2 times
4 - 1 time
6 - 1 time
what i use is
... (14 Replies)
Discussion started by: Learnerabc
14 Replies
5. Shell Programming and Scripting
I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by
awk 'NR==29,NR==10029' File1 > File2
and then doing
awk '{print $1, $2, $13, $14}' File2 > File3
Can... (3 Replies)
Discussion started by: ananyob
3 Replies
6. Shell Programming and Scripting
Hello,
I have attached one file named file.txt .
I have to create a file using the awk script with the records in which 38th position is P and not V .
ex
it should have
00501 HOLTSVILLE NYP00501
and it should not include
00501 I R S SERVICE CENTER ... (3 Replies)
Discussion started by: sonam273
3 Replies
7. Shell Programming and Scripting
Hi guys!
I'm new to scripting and I need to write a script in awk.
Here is example of file on which I'm working
ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00
ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00
ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies
8. Shell Programming and Scripting
Hi friends,
This is sed & awk type question.
I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example
###start of input text file ####
abc
def
ghi
1
2
3
4
kjld
random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies
9. UNIX for Dummies Questions & Answers
Hello,
I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file
Name m/c usage
abc xxx 10
abc xxx 20
abc xxx 5
xyz ... (6 Replies)
Discussion started by: asjaiswal
6 Replies
10. Shell Programming and Scripting
I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines:
It counts the number of lines:
egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies
LEARN ABOUT LINUX
setgrent
GETGRENT(3) Linux Programmer's Manual GETGRENT(3)
NAME
getgrent, setgrent, endgrent - get group file entry
SYNOPSIS
#include <sys/types.h>
#include <grp.h>
struct group *getgrent(void);
void setgrent(void);
void endgrent(void);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
setgrent(),
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED ||
/* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
getgrent(), endgrent():
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
DESCRIPTION
The getgrent() function returns a pointer to a structure containing the broken-out fields of a record in the group database (e.g., the
local group file /etc/group, NIS, and LDAP). The first time getgrent() is called, it returns the first entry; thereafter, it returns suc-
cessive entries.
The setgrent() function rewinds to the beginning of the group database, to allow repeated scans.
The endgrent() function is used to close the group database after all processing has been performed.
The group structure is defined in <grp.h> as follows:
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* group members */
};
RETURN VALUE
The getgrent() function returns a pointer to a group structure, or NULL if there are no more entries or an error occurs.
Upon error, errno may be set. If one wants to check errno after the call, it should be set to zero before the call.
The return value may point to a static area, and may be overwritten by subsequent calls to getgrent(), getgrgid(3), or getgrnam(3). (Do
not pass the returned pointer to free(3).)
ERRORS
EINTR A signal was caught.
EIO I/O error.
EMFILE The calling process already has too many open files.
ENFILE Too many open files in the system.
ENOMEM Insufficient memory to allocate group structure.
ERANGE Insufficient buffer space supplied.
FILES
/etc/group
local group database file
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001.
SEE ALSO
fgetgrent(3), getgrent_r(3), getgrgid(3), getgrnam(3), getgrouplist(3), putgrent(3)
COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
2010-09-20 GETGRENT(3)