Operating System - HP-UX
1752795 Members
5638 Online
108789 Solutions
New Discussion юеВ

Sort column 4 in order ...

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

Sort column 4 in order ...

Hi All,

I want to sort the below using column 4:

ent0 Available 03-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent1 Available 03-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent2 Available 05-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent3 Available 05-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent4 Available Virtual I/O Ethernet Adapter (l-lan)
ent5 Available Virtual I/O Ethernet Adapter (l-lan)
ent6 Available Virtual I/O Ethernet Adapter (l-lan)
ent7 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent8 Available Shared Ethernet Adapter
ent9 Available VLAN
ent11 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent12 Available Shared Ethernet Adapter
ent13 Available VLAN
ent14 Available VLAN


and I need the output sorted in order:

2-Port
Virtual
EtherChannel
Shared
VLAN

Thanks very much for any help.

Chris.

hello
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: Sort column 4 in order ...

Hi Chris:

For sorting on field-4 :

# sort -k4,4 file

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: Sort column 4 in order ...

hey James,

Yes that works:

ent13 Available VLAN
ent14 Available VLAN
ent9 Available VLAN
ent0 Available 03-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent1 Available 03-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent2 Available 05-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent3 Available 05-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent11 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent7 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent12 Available Shared Ethernet Adapter
ent8 Available Shared Ethernet Adapter
ent4 Available Virtual I/O Ethernet Adapter (l-lan)
ent5 Available Virtual I/O Ethernet Adapter (l-lan)
ent6 Available Virtual I/O Ethernet Adapter (l-lan)


however I need the order to be:

ent0 Available 03-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent1 Available 03-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent2 Available 05-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent3 Available 05-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent4 Available Virtual I/O Ethernet Adapter (l-lan)
ent5 Available Virtual I/O Ethernet Adapter (l-lan)
ent6 Available Virtual I/O Ethernet Adapter (l-lan)
ent11 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent7 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent12 Available Shared Ethernet Adapter
ent8 Available Shared Ethernet Adapter
ent13 Available VLAN
ent14 Available VLAN
ent9 Available VLAN


....

A bit more tricky

Chris.
hello
OldSchool
Honored Contributor

Re: Sort column 4 in order ...

well, the problem with that is that its not in any rational colating sequence.

you could sort it as noted above, then use grep to separate the components to individual files. the individual files would still be in sort order and could be "cat"ed back together in the order desired...
James R. Ferguson
Acclaimed Contributor

Re: Sort column 4 in order ...

Hi (again) Chris:

Oh, you don't have equivalent fields either in number or "content". Therefore you need to reorder your fields; sort theml and reorder them again. Something along the lines of manufacturing an additional sort key (field) and them removing it after the sort also works. A partial solution:

# awk '{if (NF>3) {print $4,$0} else {print $3,$0}}' file|sort -k4,4|cut -d" " -f 1-

...you can play with this technique...

Regards!

...JRF...
Michael Mike Reaser
Valued Contributor

Re: Sort column 4 in order ...

Lawrenzo, you're *NOT* wanting this sorted on column 4 and ONLY column 4. For several lines, you want column *3* to be the sort field, while for others you want column 4. Notice that you want "2-Port", which is field FOUR - but you also want "EtherChannel", "Shared" and "VLAN" to be your sort key, and these are field *THREE*.

You need to strip out your "sort field" information, append that to the beginning of each record, sort on field *ONE*, then strip off fields 2 thru last.
There's no place like 127.0.0.1

HP-Server-Literate since 1979
James R. Ferguson
Acclaimed Contributor

Re: Sort column 4 in order ...

Hi Chris:

I'm back, having rushed to a meeting to do "real work". Sorry, I'm mixing 'awk' and Perl's notation of counting --- 'awk' and 'cut' from one and Perl from zero. This is simplisticly close to what you want:

# awk '{if (NF==9) {print $4,$0} else {print $3,$0}}' file|sort -k1,1|cut -d" " -f 2-

Regards!

...JRF...

OldSchool
Honored Contributor

Re: Sort column 4 in order ...

"and I need the output sorted in order:

2-Port
Virtual
EtherChannel
Shared
VLAN"

which, as I stated earlier, isn't *sorted* either. Sorted order would be:

2-Port
EtherChannel
Shared
Virtual
VLAN
Hein van den Heuvel
Honored Contributor

Re: Sort column 4 in order ...

Chris, this appears to be a case where you are better of just manually going through stuff.

Still, if you want to sort something in you particular orders, which is non-alphabetic/numeric (as Old School points out) then YOU have to tell the system somehow what that order is.

For example, here is a conceptual solution in perl, ready for your refinement.

Actually... it may be all you need. It works for me :-).

-------------- tmp.pl -------------
my @order = qw ( 2-Port Virtual EtherChannel Shared VLAN ); # This defines the Order!

sub lawrenzo_special_sort {
# sort function called by perl with one line in $a
# and an other line to compare against in $b.

# empty for loop walking over pre-sorted order array and looking for the first match.
# On match, or at end, exit with loop count value in $a1

for ($a1 = 0; $a1<@order && !($a =~ /$order[$a1]/ ); $a1++) {}

# ditto for $b, setting up $b1

for ($b1 = 0; $b1<@order && !($b =~ /$order[$b1]/ ); $b1++) {}

# Now finesse the order by adding the number part of the entry to the priciple value.

$a1 += $1/100 if ($a =~ /^ent(\d+)/);
$b1 += $1/100 if ($b =~ /^ent(\d+)/);

#debug print "$a1\t$b1\t". substr($a, 0, 30) . ' | ' . substr($b, 0, 30) . "\n";

return ( $a1 <=> $b1 ); # return with comapred value.

}

# Action!

print sort {lawrenzo_special_sort} <>;

--------------------
Usage: # perl tmp.pl some-file
or: #some_command | perl tmp.pl
-----------------------------------------

ent0 Available 03-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent1 Available 03-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent2 Available 05-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent3 Available 05-09 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
ent4 Available Virtual I/O Ethernet Adapter (l-lan)
ent5 Available Virtual I/O Ethernet Adapter (l-lan)
ent6 Available Virtual I/O Ethernet Adapter (l-lan)
ent7 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent11 Available EtherChannel / IEEE 802.3ad Link Aggregation
ent8 Available Shared Ethernet Adapter
ent12 Available Shared Ethernet Adapter
ent9 Available VLAN
ent13 Available VLAN
ent14 Available VLAN

Enjoy,
Hein.




Dennis Handly
Acclaimed Contributor

Re: Sort column 4 in order ...

>OldSchool: Sorted order would be: Virtual, VLAN

Actually it would be: VLAN Virtual
Unless you used -d or -f.