![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Files and dates | mastachef | UNIX for Dummies Questions & Answers | 1 | 12-03-2007 02:12 PM |
| compare dates... | i_priyank | Shell Programming and Scripting | 3 | 09-21-2007 12:50 AM |
| compare dates | ragha81 | Shell Programming and Scripting | 2 | 11-01-2006 06:17 PM |
| comparing dates | ragha81 | Shell Programming and Scripting | 17 | 10-25-2006 05:38 PM |
| While we are on the subject of dates. Another date question | MizzGail | UNIX for Dummies Questions & Answers | 14 | 10-24-2003 07:54 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Awk question: Sum dates
Hi there,
I have a logfile which has the following layout: 20080812 0 20 20080812 12 10 20080812 12 10 20080812 12 10 I want to sum the "12" on the last 3 lines and save the "20" on the first line. The final output should be 20080812 36 20 I think that should me more easier with awk ? Many thanks in advice. |
|
||||
|
Am not sure whether I get the issue properly, but here is the code: I just assume that you will be taking the first and third fields from first line.
Code:
awk 'BEGIN {sum=0; } {if (NR==1){ var1=$1; var3=$3;} sum += $2; } END { print var1" "sum" "var3 }' filename
|
|
||||
|
Sorry thanks for your replies but i didn't put the entire file so here it is:
200808260640 0 11383 200808210640 0 200808300640 0 200808300640 0 200808300640 0 200808260640 336528522 8844 200808260640 724271039 8080 200808260640 583502861 8077 200808210640 0 200808210640 0 200808210640 0 200808290640 0 200808290640 0 200808290640 0 200808290640 0 200808150640 0 7667 200808160640 0 3285 200808310640 0 200808150640 634799861 4703 200808150640 329658775 4704 200808150640 588901581 4875 200808160640 201718658 1424 What i want to do is sum all the $2 for the same date and save the $3 for that date. E.g like a posted in the first post. 200808150640 0 7667 200808150640 634799861 4703 200808150640 329658775 4704 200808150640 588901581 4875 Expected result: 200808150640 (634799861+329658775+588901581) 7667 i want to do this for the entirely file . Many thanks in advice. |
|
|||||
|
Just guessing:
(use nawk or /usr/xpg4/bin/awk on Solaris) Code:
awk 'END \
{ for (dt in third) print dt, second[dt], third[dt] }
{ if (!_[$1]++) third[$1] = $3; second[$1] += $2 }
' filename
![]() Code:
perl -ane'
$third{$F[0]} = $F[2] unless $x{$F[0]}++;
$second{$F[0]} += $F[1];
print map "$_ $second{$_} $third{$_}\n", sort keys %x
if eof' filename
Last edited by radoulov; 09-02-2008 at 06:22 AM.. |
|
||||
|
Thank you very much my friend works like a charm.
|
| Sponsored Links | ||
|
|