LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2008, 10:21 AM   #1
lalo4080
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
awk merge and sum lines problem


I have a file containing this lines:

15 11Aug20080102 test.com/info/ zone1 test.com/
81 11Aug20080200 test.com/info/ zone1 test.com/
29 11Aug20080301 test.com/info/ zone1 test.com/
49 11Aug20080400 test.com/info/ zone1 test.com/
18 11Aug20080205 test.com/info/ zone1 test.com.mx/
23 11Aug20080316 test.com/info/ zone1 test.com.mx/

how can I merge this lines and sum the values on the first column but printing the first date ocurrence?

desired output:

174 11Aug20080102 test.com/info/ zone1 test.com/
41 11Aug20080205 test.com/info/ zone1 test.com.mx/

I just tried this code:

BEGIN{ FS = " ";}
{
data[$3" "$4" "$5" "$2] += $1
}
END{
n = asorti(data,hits)

for (i = 1; i <= n; i++)
printf "%s %s\n", data[hits[i]], hits[i]
}


but I haven't done it work, I had to left the date out of the index to sum the field...

Cheers,
EM
 
Old 08-12-2008, 10:37 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
I think you should use two arrays: one to record the first date for a key, another to record the sum. What exactly are you using as the key? Everything after the date?
 
Old 08-12-2008, 10:41 AM   #3
lalo4080
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Yes if I use the code:

BEGIN{ FS = " ";}
{
data[$3" "$4" "$5] += $1
}
END{
n = asorti(data,hits)

for (i = 1; i <= n; i++)
printf "%s %s\n", data[hits[i]], hits[i]
}

I got the desired result but without the date field.
 
Old 08-12-2008, 10:51 AM   #4
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
As I said, you should put the date into a second array, e.g.

Code:
dates[$3" "$4" "$5] = $2;
Of course, you need to check whether there is already a value associated with that key and, if so, whether it is larger or smaller than the new value.
 
Old 08-12-2008, 11:21 AM   #5
lalo4080
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Original Poster
Rep: Reputation: 0
well this code did the trick:

BEGIN{ FS = " ";}
{
data[$3" "$4" "$5] += $1
data2[$3" "$4" "$5] = $2
}
END{
n = asorti(data,hits)
m = asorti(data2,dates)

for (i = 1; i <= n; i++) {
for (i = 1; i <= m; i++) {
printf "%s %s %s\n", data2[dates[i]], data[hits[i]], hits[i]
}
}
}

Cheers,
EM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge lines in a file using sed arobic Programming 8 01-20-2012 03:11 PM
Merge lines from a file lourencojunior Linux - Newbie 5 05-12-2008 02:54 PM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 11:10 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration