Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort question

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
I have a multiple field file that I need to sort by field 1, and then by field 3 within field 1. I also have to drop the duplicates of field 1, keeping only one occurrence of field on that has the lowest value of field 2.
Can this be done with one or more executions of sort?
 
Hi Pjb,

Yes, you can!

Use sort and uniq.

Thanks,

Gyan
 
Yes, but the -u parameter will drop duplicates. These are not duplicates. In field 1 the value is the same, but in field 2, the values can differ. The file would look like this:

AAAA 1
AAAA 2
BBBB 1
BBBB 2

How would I retain only the AAAA and BBBB records that had the lowest value in field 2?
 
This is probably not the best solution but it should work if i'm understanding correctly what you're asking.

for i in `cat testfile | awk '{print $1}' | sort -u`
do
grep $i testfile | sort +0 -1 -t" " +1 -n | head -1
done
 
Assuming this is all sorted and your input is

AAAA 1
AAAA 2
BBBB 1
BBBB 2

then try

awk '{if($1!=FLD) print; FLD=$1}' input-file

which only keeps the first record based on the first field.

Cheers,
ND
 
Hi pjb,

Try :

sort -k1,3 input_file | sort -u -k1,1 Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top