The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-01-2008
BufferExploder BufferExploder is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
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.
  #2 (permalink)  
Old 09-01-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Cochin/Singapore
Posts: 515
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
  #3 (permalink)  
Old 09-02-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,318

Code:
awk '
 NR==1 { var1=$1; var3=$3; next }
 { var2 += $2 }
END { print var1" "var2" "var3 }' filename
I suspect that what you really want is more complex than this, If so, you'll probably have to describe your input file more completely.
  #4 (permalink)  
Old 09-02-2008
BufferExploder BufferExploder is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
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.
  #5 (permalink)  
Old 09-02-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,794
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
If you need the output sorted, pipe it to sort (or use asorti if you have GNU Awk). Or just use Perl

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..
  #6 (permalink)  
Old 09-02-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Cochin/Singapore
Posts: 515
Try this:

Code:
for each in $(cut -c1-8 filename | sort -u)
        do
        awk '/^'$each'/{ if ($2==0){ var1=$1;var3=$3;} sum+=$2; }END { print var1" "sum" "var3;}' filename
        done
  #7 (permalink)  
Old 09-02-2008
BufferExploder BufferExploder is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
Thank you very much my friend works like a charm.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0