Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: sort on last word in field/record

948 views
Skip to first unread message

Ken Brown

unread,
Dec 15, 1994, 11:33:33 AM12/15/94
to

Hello,

I'm having trouble sorting on the *last* word of the last field in a record.

The details;

number of fields = 2
Field separator = :
First field alsways has one word
Second field has variable number of words

Example:

adjn:Joshua Ng
adktk:KaLap Timothy Kwong
admg:Mahalingam Gobieramanan
admln:Martha L. Nangalama
admrp:Michael R. Pearson
adshl:Siu Lung Henry Lee
adsyh:So Yi Ho
adtkk:Tariq K. Khan
adtkl:Terence K. H. Leung
advld:Vickie L. Dwyer
alaen:Alfred E. Neuman
anedp:Erin D. Picard

I want to sort on the last word of field #2.

I haven't been able to figure out how to do this with sort(1). Any awk,
sed, perl or other suggestions and/or pointers would be most welcome.

Thanks!
--
Ken Brown internet: kbr...@trentu.ca
Trent University Computing & Telecommunications tel: (705)748-1540
Peterborough, Ontario, Canada, K9J 7B8 fax: (705)748-1635

DFRussell

unread,
Dec 15, 1994, 1:36:55 PM12/15/94
to
awk 'BEGIN {OFS=":"}
{
print $NF, $0
}' your_filename | sort -t: +0 -1 | cut -d: -f2-3

--
Disclaimer: I don't speak for Martin Marietta or the EPA.
----------------------------------------------------------
dfru...@unixmail.rtpnc.epa.gov, Martin Marietta TSI,
P.O. Box 14365, MD-4501-1B, Research Triangle Park, NC 27709

Edgar R. Allen

unread,
Dec 15, 1994, 7:16:28 PM12/15/94
to
In article <1994Dec15.1...@blaze.trentu.ca> cc...@dakhma.trentu.ca (Ken Brown) writes:
>
>Hello,
>
>I'm having trouble sorting on the *last* word of the last field in a record.
>
>The details;
>
>number of fields = 2
>Field separator = :
>First field alsways has one word
>Second field has variable number of words
>
>Example:
>
>adjn:Joshua Ng
>adktk:KaLap Timothy Kwong
>admg:Mahalingam Gobieramanan
>admln:Martha L. Nangalama
>admrp:Michael R. Pearson
>adshl:Siu Lung Henry Lee
>adsyh:So Yi Ho
>adtkk:Tariq K. Khan
>adtkl:Terence K. H. Leung
>advld:Vickie L. Dwyer
>alaen:Alfred E. Neuman
>anedp:Erin D. Picard
>
>I want to sort on the last word of field #2.
>
>I haven't been able to figure out how to do this with sort(1). Any awk,
>sed, perl or other suggestions and/or pointers would be most welcome.
>
Two seds and a sort.

sed 's/\(.* \)\(.*\)/\2:\1/' /tmp/tst | sort -t: | sed 's/\([^:]*\):\(.*\)/\2\1/'


Ed Allen
--
--
i don't really KNOW, |Ed Allen
|Wilcox Electric Inc.
i just make it up as i go |2001 NE 46 Street
|Kansas City Missouri, 64116

Randal L. Schwartz

unread,
Dec 16, 1994, 11:24:18 AM12/16/94
to

[comp.lang.perl included... watch your followups]

>>>>> "Ken" == Ken Brown <cc...@dakhma.trentu.ca> writes:

Ken> Hello,

Ken> I'm having trouble sorting on the *last* word of the last field in a record.

Ken> The details;

Ken> number of fields = 2
Ken> Field separator = :
Ken> First field alsways has one word
Ken> Second field has variable number of words

Ken> Example:

Ken> adjn:Joshua Ng
Ken> adktk:KaLap Timothy Kwong
Ken> admg:Mahalingam Gobieramanan
Ken> admln:Martha L. Nangalama
Ken> admrp:Michael R. Pearson
Ken> adshl:Siu Lung Henry Lee
Ken> adsyh:So Yi Ho
Ken> adtkk:Tariq K. Khan
Ken> adtkl:Terence K. H. Leung
Ken> advld:Vickie L. Dwyer
Ken> alaen:Alfred E. Neuman
Ken> anedp:Erin D. Picard

Ken> I want to sort on the last word of field #2.

Ken> I haven't been able to figure out how to do this with sort(1). Any awk,
Ken> sed, perl or other suggestions and/or pointers would be most welcome.

Speak with a lisp in Perl, I'd write it like this:

#!/usr/bin/perl
require 5; # new features, new bugs!
print
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_, /(\S+)$/] }
<>;

print "Just another Perl hacker,"
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <mer...@stonehenge.com> Snail: (Call) PGP-Key: (finger mer...@ora.com)
Phrase: "Welcome to Portland, Oregon ... home of the California Raisins!"

D. Jaeckel

unread,
Dec 18, 1994, 5:01:31 PM12/18/94
to
mer...@stonehenge.com (Randal L. Schwartz) writes:

>Speak with a lisp in Perl, I'd write it like this:

> #!/usr/bin/perl
> require 5; # new features, new bugs!
> print
> map { $_->[0] }
> sort { $a->[1] cmp $b->[1] }
> map { [$_, /(\S+)$/] }
> <>;

Please explain, how it works!
I tried to figure out, but I didn't get it.

I'm not yet very familiar with the *special* features of perl5.
How would you do it in perl4?

Thanks Dirk
--
biafra@acab.[isdn.cs.tu|in]-berlin.de (Dirk Jaeckel)
GCS d--->---- H s+:- g+ !p au* a- w+ v(?) C++ USL++++$ P+++ L++
3+>++ E--- N++>+++ K- W---- M !V p--- Y+>++ t++@ 5- !j G?
tv++>! b++ D--- B? e+ u* h+(--) f+ r- !n y?

0 new messages