The UNIX and Linux Forums  

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
need help in sort ali560045 Shell Programming and Scripting 2 12-04-2007 07:38 AM
sort prasathlogu UNIX for Dummies Questions & Answers 1 10-08-2007 07:56 AM
sort help unics Shell Programming and Scripting 14 04-30-2007 08:28 AM
du -h | sort ? fongthai Shell Programming and Scripting 6 11-02-2006 08:59 PM
Sort Help! kev112 Shell Programming and Scripting 3 05-30-2005 04:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-06-2007
shotronix shotronix is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 2
sort and summarize

Hi Guys,

I have a file in UNIX with duplicates, I have use sort command as below to delete duplicates based on the KEY positions/columns but now I do not want to "delete" duplicates but summarize by KEY numeric columns.

REALLY NEED HELP... URGENT!!!

Thanks in advance.

sort -k 1.1,1.92 -u file > outfile
  #2 (permalink)  
Old 12-06-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Question

I don't think sort does that natively...

If you can provide an example input and an example output showing what you want done, it's probably scriptable.
  #3 (permalink)  
Old 12-06-2007
shotronix shotronix is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 2
Here is the example:

1288M99G14 ALA201001+00000000.000+00000005.000
1288M99G14 ALA201001+00000000.000+00000005.000
1288M99G14 ALB201001+00000005.000+00000000.000
1288M99G14 ALA201002+00000000.000+00000017.000
1288M99G14 ALB201001+00000017.000+00000000.000
1288M99G14 ALA201002+00000000.000+00000005.000

Output:

1288M99G14 ALA201001+00000000.000+00000010.000
1288M99G14 ALB201001+00000023.000+00000000.000
1288M99G14 ALA201002+00000000.000+00000023.000

So summarize by first 2 fields
  #4 (permalink)  
Old 12-06-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Post

Ah, so it's totalling them...

Sounds like a awk or perl solution would be the way to go.
You can then pipe the ouput through sort to get whatever order you want. You've already got the sort right (without the -u of course) so I'll focus on the totaling part...

As I'm not great with awk, I'll try perl, I'm sure one of the awk wizzes around here can offer up a solution for that

Code:
#!/bin/perl -w

while (<>) {
  ($name,$left,$right)=split(/\+/);
  $vals{$name}{"left"}+=$left;
  $vals{$name}{"right"}+=$right;
}

foreach $name (keys %vals) {
  printf "%s\+%012.3f\+%012.3f\n",${name},$vals{$name}{'left'},$vals{$name}{'right'};
}
  #5 (permalink)  
Old 12-07-2007
ranjithpr ranjithpr is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 157
Try this one

filename=$1
sort $filename|
awk ' BEGIN {FS="+"; prev_key1=""; prev_key2=0; prev_key2=0; first=1; }
{
# print "asdfdafsdfsdfasf|"prev_key1 "|"$1
if($1==prev_key1)
{
prev_key2 += $2;
prev_key3 += $3;
}
else
{
if(!first)
printf("%20.20s+%08.3f+%08.3f\n",prev_key1,prev_key2,prev_key3);
else first=0;
prev_key1 = $1;
prev_key2 = $2;
prev_key3 = $3;
}
}
END {printf("%20.20s+%08.3f+%08.3f\n",prev_key1,prev_key2,prev_key3);}'
  #6 (permalink)  
Old 12-09-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
Quote:
Originally Posted by ranjithpr View Post
filename=$1
sort $filename|
awk ' BEGIN {FS="+"; prev_key1=""; prev_key2=0; prev_key2=0; first=1; }
...
The sort needs to be smarter, the OP was not sorting by the first element (but they have that bit working so I just left it out of the solution )
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:34 AM.


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