The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sort file alphabetically AND numerically mrodrig UNIX for Dummies Questions & Answers 2 05-19-2008 10:46 AM
Sort a file line by line alphabetically H2OBoodle Shell Programming and Scripting 11 02-11-2008 07:27 AM
How to sort decimal values in bash ahjiefreak Shell Programming and Scripting 1 01-21-2008 09:25 AM
finding specific values in a within a file Gerry405 UNIX for Dummies Questions & Answers 3 11-21-2005 11:37 AM
How to Sort files based on predefined values.? p_prathaban Shell Programming and Scripting 2 02-17-2004 11:39 AM

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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 04-27-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
How to sort alphabetically after finding values

I have a list of people in a usage log and need to print the names and phone numbers of people with over 500 logins. I'd also like to display these names alphabetically.

I have their total logins set to a variable named total.

So far, I have very little in my awk script to do this:

FS=":"
{if ( total > 500 ) print $1, $2}

($1 being both first and last name, $2 being phone number)

There is more to the script than this, but above is the relevant portion.

This only prints one of their names unfortunately, even though my data file clearly has others with totals > 500.

Any advice or pointers here? Thanks!
  #2 (permalink)  
Old 04-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Sounds like the problem would be in other parts of the logic. Is total calculated once for the whole file, and you run it again and again for each user, or how do you end up with that total?

A common technique is to keep an array of totals where the keys are the users, so you only have to run over the file once.

Code:
awk -F : '{ if (++total[$1] > 500) print $1, $2 }' file
(This will print multiple times, once for each record after the total is exceeded. Figuring out how to avoid that is left as an exercise.)

Once you get it to print what you want, just pipe that output to sort.

Last edited by era; 04-27-2008 at 05:58 PM.. Reason: Code example
  #3 (permalink)  
Old 04-27-2008
doubleminus doubleminus is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 27
Yes, total is calculated above outside of the END statement:
total = $3 + $4 + $5

($3-$5 being fields showing logins for each of three months)
  #4 (permalink)  
Old 04-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
So you have one row per user, not a log with one row per log-in? You're not printing in the END statement then, are you? END only gets evaluated once, at end of file.
  #5 (permalink)  
Old 04-28-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,315
To make the story short, post your input and the desired output.
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 04:14 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