Re: Sort column 4 in order ... (129 Views)
Reply
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 1 of 13 (129 Views)
Accepted Solution

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 13 (129 Views)

Re: Sort column 4 in order ...

Hi Chris:

For sorting on field-4 :

# sort -k4,4 file

Regards!

...JRF...
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 3 of 13 (129 Views)

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
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 4 of 13 (129 Views)

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...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 13 (129 Views)

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...
Please use plain text.
Valued Contributor
Michael Mike Reaser
Posts: 122
Registered: ‎08-31-2007
Message 6 of 13 (129 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 13 (129 Views)

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...

Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 8 of 13 (129 Views)

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
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,581
Registered: ‎05-19-2003
Message 9 of 13 (129 Views)

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.




Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 10 of 13 (129 Views)

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.
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 11 of 13 (129 Views)

Re: Sort column 4 in order ...

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

Nice catch...although I didn't use either option.

I *did* run it on a Linux box that, for some reason, didn't have LC_ALL set....

which also explains why a couple of the developers are complaining about some scripts they are porting (which depend on "tr" working normally)
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 12 of 13 (129 Views)

Re: Sort column 4 in order ...

just back after the weekend so thanks all for the suggestions and notes,

I'll take a look this morning and let you know the solution and lots to attempt.

Thanks again.

Chris.
hello
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 13 of 13 (129 Views)

Re: Sort column 4 in order ...

thanks all
hello
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation