For your sample input, where you want to perform an alphabetic sort on the 1st two characters of the 2nd field as the primary key, a numeric sort on the remaining characters in the 2nd field as the secondary key, and an alphanumeric sort on the 1st field as the tertiary key, the following should work:
If the length of the alphabetic strings at the start of the 2nd field is variable length, or if you also want to split the 1st field into alpha and numeric portions and sort them as separate keys as well, the following should work:
This was written and tested using a Korn shell, but should work with any POSIX-conforming shell. If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
The above should work on any system. Some versions of sort have simpler ways of sorting alphabetic and numeric parts of individual fields and some versions of awk have built-in sort features; but since you didn't bother telling us what operating system you're using, I limited my response to more portable code.
These 2 Users Gave Thanks to Don Cragun For This Post:
I want to sort my data first by the 2nd field then by the first field.
I can't use sort -V because I don't have gnu sort and cannot install one.
How do I go about this?
Input:
G456 KT1 34
K234 KT10 45
L2 KT2 26
H5 LAF2 28
F3 LAF2 36
Output:
G456 KT1 34
L2 KT2 26
K234 KT10 45
F3... (14 Replies)
I have files like this:
1
3
4
6
14
3
6
I want to extract the highest number. I have tried using
cat filename | sort
but then 9 would become higher than 14.
So how do I sort? (1 Reply)
Hello,
okey so my script is using 4 variables that are either empty or numbers in the following format:
NUMBER_1 NUMBER_2 NUMBER_3 NUMBER_4
So they're basically separated by a space and I need to echo the lowest number, so far I've been doing it like this:
echo "2 3 1 3" | tr " "... (6 Replies)
Hi All,
Sorry to say I have 0 experience writing C++ but have been asked to write a piece of code that will take a double input and an integer for number of decimal places as well as integer for padding and output a string that represents the double formatted (with comma thousand separators -... (2 Replies)
Hi to all.
I'm trying to sort this with the Unix command sort.
user1:12345678:3.5:2.5:8:1:2:3
user2:12345679:4.5:3.5:8:1:3:2
user3:12345687:5.5:2.5:6:1:3:2
user4:12345670:5.5:2.5:5:3:2:1
user5:12345671:2.5:5.5:7:2:3:1
I need to get this:
user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Hy guys. My English is not so good, sorry for any mistakes.
I'm a bigginer in C, and I have a problem. I want to sort ascending n strings, but I can't read the strings. Here is what I've done so far:
//sort ascending n strings
#include <stdio.h>
int main()
{
int n,i,j;
char a;
... (8 Replies)
I'm trying to find the longest word in /usr/share/dict/words
The first thing I can think of is to sort the content by length then it would be easy to find out, but then i realize theres no option of sort to sort by length.
Could you guys please give me some help?:confused: (7 Replies)
I tried something like:
set test3 = (4.985e-10 5.130e-10 5.486e-10 6.023e-10 7.015e-10)
set test4 = (4.869e-10 5.010-10 5.363e-10 5.895e-10 6.887e-10)
set test5 = $test3 - $test4
but this doesn't seem to work. And then I tried:
@ test5 = $test3 - $test4
This doesn't seem to work... (8 Replies)
Hello all,
I am being dumb with this and I know there is a simple solution.
I have a file with the follwing lines
bc stuff (more)...............123
bc stuffagain (moretoo)............0
bc stuffyetagain (morehere)......34
failed L3 thing..............1
failed this... (2 Replies)