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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Aggregate title to an archive.log enkei17 UNIX Desktop for Dummies Questions & Answers 1 08-17-2008 07:55 AM
Aggregate values in a file & compare with sql output shiroh_1982 UNIX for Dummies Questions & Answers 1 09-02-2007 07:40 AM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
aggregate ethernet ports under Solaris 98_1LE UNIX for Dummies Questions & Answers 4 02-13-2002 03:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-26-2008
anaconga anaconga is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
AWK aggregate records

Hy all,

I have a problem...can some one help me...


I have a file of records sort:
30|239|ORD|447702936929 |blackberry.net |20080728|141304|00000900|2|0000000000000536|28181|0000000006|0000000001|10|1

30|239|ORD|447702936929 |blackberry.net |20080728|141304|00000340|2|0000000000007076|29211|0000000024|0000000007|10|1

30|239|ORD|447702936929 |blackberry.net |20080728|141305|00000900|3|0000000000001568|28181|0000000002|0000000002|10|1

What i want to do is aggregate this records by the following conditions

In tha case that the record have the same key ($1, $4, $5) and the field $9 it's =2:

VAR= $7 + $8 + 2

Next i go to the next record and:
if field $7 <= VAR, then i aggregate this two records

else...next record...

the last record need to have the $9 = 3, because itīs indicate that my leg is closed.

i just can aggregate when i have a multiple records with $9=2 and the last record with $9=3.

the result of the example it's something like this:

30|239|ORD|447702936929 |blackberry.net |20080728|141304|00002140|1|0000000000000536|28181|0000000006|0000000001|10|1

When i aggregate the record, i sum the field $8 and set the $9 with 1.
  #2 (permalink)  
Old 08-27-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
I need a little more info. After aggregating, what do you want to print out? When there's no aggregation, do you want to print out anything?

Here's a start:
Code:

awk -F\| '
  VAR && $7 <= VAR { AGGREGATE HERE; VAR=0; next; }
  $9 == 2 && $1 == $4 && $1==$5 { VAR=$7+$8+2; next; }
  { PRINT OTHER LINES HERE }
'
  #3 (permalink)  
Old 08-27-2008
anaconga anaconga is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
hello....

this is what i deed....

I donīt know if itīs the best idea...but works....

your sugestion do something like this?

BEGIN {
FS="|";
c1 = -1;
c4 = -1;
c5 = -1;
c8 = 0;
time = 0;
}
{
if (($1 == c1) && ($4 == c4) && ($5 == c5))
{
# accumulate value $8
if ($7 <= time)
{
c8 = c8 + $8
# updates variable time
time = $7 + $8 + 2
}
#write to output
if ($9 == 4)
{
print c1"|"c2"|"c3"|"c4"|"c5"|"c6 "|"c7 "|"c8"|1|"c10"|"c11"|"c12"|"c13"|"c14"|"c15 >> file_complete
close(file_complete)
#restart variables
time = 0
c8 = 0
next
}
}
else
{ # the record don't have the same key has the last record
if (c1 == -1)#when reads the 1st line of the file only keeps the relevant fields of the first record
{
if ($9 == 2) #save the fields from the beginning of the call, to put in output
{
c1 = $1
c2 = $2
c3 = $3
c4 = $4
c5 = $5
c6 = $6
c7 = $7
c8 = $8
c10 = $10
c11 = $11
c12 = $12
c13 = $13
c14 = $14
c15 = $15
time = $7 + $8 + 2
next
}
}
if (c1 != -1)#print output of the previous
{
print c1"|"c2"|"c3"|"c4"|"c5"|"c6"|"c7"|"c8"|1|"c10"|"c11"|"c12"|"c13"|"c14"|"c15 >> file_complete
close(file_complete)
# save the current record if is the beginning of the call
if ($9 == 2)
{ # save the fields from the beginning of the call, to put in output
c1 = $1
c2 = $2
c3 = $3
c4 = $4
c5 = $5
c6 = $6
c7 = $7
c8 = $8
c10 = $10
c11 = $11
c12 = $12
c13 = $13
c14 = $14
c15 = $15
time = $7 + $8 + 2
}
}
}
}
END {
#print the output for the case that not caught the last registration with closing ($ 9 = 4, but caught with $ 9 = 3)
if ($9 == 3)
{
print c1 "|" c2 "|" c3 "|" c4 "|" c5 "|" c6 "|" c7 "|" c8 "|1|" c10 "|" c11 "|" c12 "|" c13 "|" c14 "|" c15 >> file_complete
close(file_complete)
}
}
  #4 (permalink)  
Old 08-27-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
It looks like you understand what you're doing.

You can save yourself a lot of time by setting OFS to "|" and just doing print $0 instead of every variable independently. Also, while you can do it in one big awk program, its easier -- syntactically -- to break it up into multiple awk programs. Now, I don't mean multiple instances of awk. Every awk invocation can contain multiple programs, like this:
Code:
awk 'BEGIN { FS="/" }  <condition1> { program1... } <condition2> { program2}'
If condition1 matches, program1 will run. If condition2 matches, program2 will run (regardless of whether program1 ran or not). If you want program1 to stop processing and go to the next line, you use "next;". If you want program2 to get the next line now, you can do "getline;". (That might be GNU specific.)

Also, when you post on this forum, it helps to embed your code in [code] tags. It keeps the spacing.
  #5 (permalink)  
Old 08-27-2008
anaconga anaconga is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
sorry....iīm new in this things.....

About your sugestion, it seams very good ....i will try split my AWK in another AWKs....

thanks...
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 10:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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