Operating System - HP-UX
1833018 Members
2032 Online
110048 Solutions
New Discussion

Re: sort with -u unique option

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

sort with -u unique option

I have been looking at this for an hour now and cannot see what I am doing wrong. I use the following commands to extract four fields from all of our various passwd files, and I only want one line returned for each logonid:

cat $secdir/*.passwd | awk -F: '{print $1,$3,$4,$5}'

Here is an example of the raw data for one logonid:

cat $secdir/*.passwd | awk -F: '{print $1,$3,$4,$5}' | sort|grep best1
best1 1100 1100 best1
best1 1100 1100 best1
best1 1100 1100 best1
best1 1100 1100 best1
best1 1100 1100 best1
best1 1100 1100 best1
best1 1500 550 Application ID,PDC,,
best1 1500 550 Best1,,,


When I run the following command:

cat $secdir/*.passwd | awk -F: '{print $1,$3,$4,$5}' | sort -u -k1

I would expect to get only one line back (if I understand the -u option correctly from the man page). But instead I get:

best1 1100 1100 best1
best1 1500 550 Application ID,PDC,,
best1 1500 550 Best1,,,


It's as if the -u was being applied to the entire line, rather than just the first key field.

Here is the section of the man page for sort I am relying on:

-u Unique: suppress all but one in each set of lines
having equal keys.

Any ideas?

Scott
6 REPLIES 6
Patrick Wallek
Honored Contributor

Re: sort with -u unique option

If you just want the unique login id's, why do you need the other 4 fields?

Why not just do:

cat $secdir/*.passwd | awk -F: '{print $1}' | sort -u

I don't really see what you are gaining, or trying for, by including the other information.

Ivan Ferreira
Honored Contributor
Solution

Re: sort with -u unique option

I tried with sort -k 1,1 -u and works.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Scott Lindstrom_2
Regular Advisor

Re: sort with -u unique option

Patrick -

I need the other four fields (UID, GID, etc) for the report that is eventually generated, but only one line per logonid.

Scott
Scott Lindstrom_2
Regular Advisor

Re: sort with -u unique option

Ivan -

sort -k1,1 was all that was needed!

I 'assumed' wrong that 'sort -k1' only sorted on the first key. In reality (from the sort man page):

"A missing field_end means the end of the line."

Thanks!
Scott
James R. Ferguson
Acclaimed Contributor

Re: sort with -u unique option

Hi Scott:

Yes, your specification for the sort key is actually until the end-of-line.

This is a common mistake. You need to specify both the start and the end of the key:

# sort -u -k1,1

Regards!

...JRF...
Scott Lindstrom_2
Regular Advisor

Re: sort with -u unique option

sort -k1,1 was all that was needed!