Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!wuarchive!udel!haven.umd.edu!mimsy!mojo!eng.umd.edu!ziegast From: ziegast@eng.umd.edu (Eric W. Ziegast) Newsgroups: comp.unix.shell Subject: Re: How to sort on right most column Message-ID: <1991May15.150035.29591@eng.umd.edu> Date: 15 May 91 15:00:35 GMT References: <13320@exodus.Eng.Sun.COM> Sender: news@eng.umd.edu (C-News) Reply-To: ziegast@eng.umd.edu (Eric W. Ziegast) Organization: College of Engineering, Maryversity of Uniland, College Park Lines: 49 Off the top of my head I did: #!/usr/bin/perl # Read in data from STDIN (or specified files) in to @lines @lines = (<>); # Subroutine to numerically compare last space-separated fields. sub rightmost { local(@a) = split(" ",$a); local(@b) = split(" ",$b); $a[$#a] <=> $b[$#b]; } foreach (sort rightmost @lines) { print; } After feeding it the following as standard input: FOO BAR ACE CORPORATION SUNNYVALE 2.00 FOO BAR ACER COMPUTED COMPANY MILPITAS 20.00 FOO BAR APOLLO COMPUTER, INC. CHELMSFORD 1.00 FOO BAR APPLE COMPUTER, INC. CUPERTINO 8.00 FOO BAR BOEING TUKWILA 53.00 FOO BAR BOEING COMPUTER SERVICES EDDYSTONE 2.00 FOO BAR CITIBANK N. A. ANDOVER 4.00 FOO BAR CITIBANK NORTH AMERICA LONG ISLAND CITY 26.00 I got back: FOO BAR APOLLO COMPUTER, INC. CHELMSFORD 1.00 FOO BAR ACE CORPORATION SUNNYVALE 2.00 FOO BAR BOEING COMPUTER SERVICES EDDYSTONE 2.00 FOO BAR CITIBANK N. A. ANDOVER 4.00 FOO BAR APPLE COMPUTER, INC. CUPERTINO 8.00 FOO BAR ACER COMPUTED COMPANY MILPITAS 20.00 FOO BAR CITIBANK NORTH AMERICA LONG ISLAND CITY 26.00 FOO BAR BOEING TUKWILA 53.00 There's probably a better way of doing this (isn't there always?), but this is probably the most direct. -- +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+ | Eric W. Ziegast Internet: ziegast@eng.umd.edu | | Univ. of Merryland Phonenet: Eric@[301.405.3689] | +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+