How to sort alphabetically after finding values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to sort alphabetically after finding values
# 1  
Old 04-27-2008
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  
Old 04-27-2008
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  
Old 04-27-2008
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  
Old 04-27-2008
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  
Old 04-28-2008
To make the story short, post your input and the desired output.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort alphabetically starting from specified letter

Hi. I'm trying to sort a list of items in a file alphabetically but starting from the letter 'X'. For instance if I had the following file; test.txt Z A T W Y B S X I would like the output to look like; X Y (8 Replies)
Discussion started by: mmab
8 Replies

2. Shell Programming and Scripting

Using awk to sort a file alphabetically

I have a problem with my homework I need to create a shell script using #!bin/awk -f the script will output the file in an alphabetical order only words and after the word is : after that a space then , then it will be numbered each character by which line its been for example CB 92A A... (1 Reply)
Discussion started by: collapse
1 Replies

3. Shell Programming and Scripting

Finding the right file with multiple sort criteria

Hello, I have files in a directory with names like, ./f0/84.40_E1200_85.39_E1300_f0_r00_1300-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.40_E1200_85.83_E1200_f0_r00_1200-ON-0.25_S7A_v4_47.19.1.out.txt ./f0/84.60_E1100_86.45_E1100_f0_r00_1100-ON-0.25_S7A_v4_47.19.1.out.txt... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Reverse the order of a list of file names (but not sort them alphabetically or numerically)

Hello all, I have a list of file names in a text document where each file name consists of 4 letters and 3 numbers (for example MACR119). There are 48 file names in the document (they are not in alphabetical or numerical order). I would like to reorder the list of names so that the 48th name is... (3 Replies)
Discussion started by: MDeBiasse
3 Replies

5. Shell Programming and Scripting

Finding change in values

I have an array X = ( -100 -90 -80 -70 -60 -50 -40 30 40 50 60 70 80 90 100 ) I want to find the place where values change from negative to positive. (8 Replies)
Discussion started by: kristinu
8 Replies

6. Shell Programming and Scripting

Sort alphabetically, then numerically

Greetings - I'm not necessarily new to bash scripting - I'm probably between beginner and intermediate, but I have something that I just cannot figure out after many attempts to find it. I have a file that is merely a list of many files, with their respective paths, and a branch path (ClearCase)... (5 Replies)
Discussion started by: 1cor29
5 Replies

7. Shell Programming and Scripting

Sort by numbers, then alphabetically

Hey guys, I have a file that contains the following: 366 K 364 Q 12 UB 7 INC. P 4 Law 2 LAMB 2 High 1 QEG 1 OF 1 LC 1 B As you can see, it's already sorted by numerical order, how do I sort it again, breaking the ties by using the alphabetical order of the second column, but... (2 Replies)
Discussion started by: Andrew9191
2 Replies

8. Shell Programming and Scripting

finding values in between

Hi, I have been trying to find someone with this similar problem but I was out of luck. So I have a file that has two columns that look like this (for example): 10 20 40 50 45 60 90 130 So column 1 is start and column 2 is stop but what I want to do is find whats not represented... (4 Replies)
Discussion started by: phil_heath
4 Replies

9. UNIX for Dummies Questions & Answers

Sort file alphabetically AND numerically

Hi all. I have 2 files like this: f1 A 10 B 80 C 9 f2 A 11 B 700 C 10 What I want is the concatenation of the two files sorted by name (alphabetically) and size (numerically), so the result should be like this: F3 (cat f1 f2 sorted) A 10 A 11 B 80 B 700 (2 Replies)
Discussion started by: mrodrig
2 Replies

10. Shell Programming and Scripting

Sort a file line by line alphabetically

infile: z y x c b a desired output: x y z a b c I don't want to sort the lines into this: a b c x y z nor this: c b a z y x The number of fields per line and number of lines is indeterminate. The field separator is always a space. Thanks for the use of your collective brains.... (11 Replies)
Discussion started by: H2OBoodle
11 Replies
Login or Register to Ask a Question