Awk question: Sum dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk question: Sum dates
# 1  
Old 09-01-2008
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  
Old 09-01-2008
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  
Old 09-02-2008

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  
Old 09-02-2008
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  
Old 09-02-2008
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 Smilie

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 07:22 AM..
# 6  
Old 09-02-2008
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  
Old 09-02-2008
Thank you very much my friend works like a charm. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell program question with Filenames and dates

Hi Unix Gurus Let's say I have the input files like the following. I need to pick the files based on my run date. abcd_20180206.csv abcd_20180213.csv abcd_20180220.csv abcd_20180227.csv efgh_20180206.csv efgh_20180220.csv efgh_20180227.csv ijkl_20180206.csv ijkl_20180213.csv... (9 Replies)
Discussion started by: SK123
9 Replies

2. Shell Programming and Scripting

awk and sum

This is my file vol0 285GB vol0.snapshot 15GB vol11_root 0GB vol12_root 47GB vol12_root.snapshot 2GB I need the output vol0 285GB,vol0.snapshot 15GB,sum-300GB vol11_root 0GB,nosnap,sum-0Gb vol12_root 47GB,vol12_root.snapshot 2GB,49GB I was trying to use paste -d, --. But... (9 Replies)
Discussion started by: ranjancom2000
9 Replies

3. Shell Programming and Scripting

Sum duplicate values in text file through awk between dates

I need to sum values in text file in case duplicate row are present with same name and different value below is example of data in file i have and format i need. Data in text file 20170308 PM,U,2 PM,U,113 PM,I,123 DA,U,135 DA,I,113 DA,I,1 20170309 PM,U,2 PM,U,1 PM,I,123 PM,I,1... (3 Replies)
Discussion started by: Adfire
3 Replies

4. Shell Programming and Scripting

awk comparison of dates

I need to use awk to return lines in multiple files that contain a date between a start date and end date. The format of the date is as seen in column 3 in the following line. A,1458147240,Mar 30 2015 12:54:00PM,s15u4chn ,2,GPS Major Alarm `clear`,component.Channel,10,15,0,138,183,,,Mar 16... (4 Replies)
Discussion started by: randman1
4 Replies

5. UNIX for Dummies Questions & Answers

awk question about sum

Hi everyone, i need help with a simple task. I have a file withe the format: "01/20/2012 23:10:13.979","49","49","48","19" "01/20/2012 23:15:13.969","47","47","48","18" "01/20/2012 23:20:13.975","47","47","45","17" "01/20/2012 23:25:13.980","44","44","44","17" "01/20/2012... (3 Replies)
Discussion started by: civilianwarfare
3 Replies

6. Shell Programming and Scripting

Sum using awk

Hi all, I need to sum values for fields in a delimited file as below: 2010-03-05||| 2010-03-05|||123 2010-03-05|467.621|369.532| 2010-03-06||| 2010-03-06||2| 2010-03-06|||444 2010-03-07||| 2010-03-07||| 2010-03-07|655.456|1019.301| Code used is: nawk -F "|" ' { sum +=... (7 Replies)
Discussion started by: Katabatic
7 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Question on reading dates in folders/files

Hi All, I have some folder that are named "FOLDERYYYYMM". I'm trying to piece together a .sh script that will look for the folder with the date. How can I get shell to see the date? (3 Replies)
Discussion started by: bbbngowc
3 Replies

9. Shell Programming and Scripting

Sum with awk

Hi,consider this fields, $1 $2 $3 981 0 1 984 0 4 985 1 0 987 0 2 990 0 0 993 0 3 995 2 0 996 0 1 999 0 4 for each occurence of zero in column $2 and $3 I need to sum $1 fields, so for example, in this piece of code the result of $1 is 8910. I'm sure... (2 Replies)
Discussion started by: cv313x
2 Replies

10. UNIX for Dummies Questions & Answers

While we are on the subject of dates. Another date question

This goes deeper into the date thing. I want to be able to check the date and time stamp in or on a file to see what the time span is. We have a job that runs several times an hour - kicked off through cron based on a trigger file. We want to keep track of each run and check the time between... (14 Replies)
Discussion started by: MizzGail
14 Replies
Login or Register to Ask a Question