Sort strings containing numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort strings containing numbers
# 1  
Old 06-02-2016
Sort strings containing numbers

How can I sort this, first by 2nd field then by 1st field.

tried
Code:
sort -b -k 2,2

Input:
Code:
AS11 AB1
BD34 AB10
AF12 AC2
A345 AB10
R134 AB2
456  AC10
TTT2 BD12

desired output:
Code:
AS11 AB1
R134 AB2
A345 AB10
BD34 AB10
AF12 AC2
456  AC10
TTT2 BD12

# 2  
Old 06-02-2016
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:
Code:
sort -k2.1b,2.2b -k2.3bn,2 -k1,1 Input

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:
Code:
#!/bin/ksh
TMPF=${0##*/}.$$
awk -v tmpf="$TMPF" '
BEGIN {	sort = "sort -t, -k3,3 -k4b,4bn -k1,1 -k2b,2bn > \"" tmpf "\""
}
{	match($1, /[0-9]/)
	printf("%s,%s,", substr($1, 1, RSTART - 1), substr($1, RSTART)) | sort
	match($2, /[0-9]/)
	printf("%s,%s,%d\n", substr($2, 1, RSTART - 1), substr($2, RSTART), NR) | sort
	l[NR] = $0
}
END {	close(sort)
	FS = ","
	while((getline < tmpf) == 1)
		print l[$5]
	close(tmpf)
}' Input
rm -f "$TMPF"

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:
# 3  
Old 06-02-2016
Hi.

Utility msort recognizes a hybrid string as in this example:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate comparing hybrid strings, msort.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort pass-fail

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat expected-output.txt

pl " Results:"
msort -qj --line -n 2,2 --comparison-type hybrid -n 1,1 --comparison-type hybrid $FILE |
tee f1

pass-fail f1 expected-output.txt

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.4 (jessie) 
bash GNU bash 4.3.30
msort 8.53
pass-fail - ( local: RepRev 1.2, ~/bin/pass-fail, 2012-06-14 )

-----
 Input data file data1:
AS11 AB1
BD34 AB10
AF12 AC2
A345 AB10
R134 AB2
456  AC10
TTT2 BD12

-----
 Expected output:
AS11 AB1
R134 AB2
A345 AB10
BD34 AB10
AF12 AC2
456  AC10
TTT2 BD12

-----
 Results:
AS11 AB1
R134 AB2
A345 AB10
BD34 AB10
AF12 AC2
456  AC10
TTT2 BD12

-----
 Comparison of 7 created lines with 7 lines of desired results:
 Succeeded -- files have same content.

( Note that pass-fail is a local command, replace it with cmp, diff, etc. )

The msort code was in GNU/Debian repository, as well as in Fedora, Ubuntu, MacOS (port), to mention a few. Also at MSORT

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort strings with numbers

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)
Discussion started by: aydj
14 Replies

2. Shell Programming and Scripting

how to sort numbers

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)
Discussion started by: locoroco
1 Replies

3. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

4. Shell Programming and Scripting

Sort numbers

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)
Discussion started by: TehOne
6 Replies

5. Programming

C++ Formatting Numbers to Strings

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)
Discussion started by: Leedor
2 Replies

6. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

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)
Discussion started by: daniel.gbaena
7 Replies

7. Programming

Sort ascending n strings in C

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)
Discussion started by: 1/0
8 Replies

8. Shell Programming and Scripting

how to sort strings by length?

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)
Discussion started by: rockbike
7 Replies

9. UNIX for Dummies Questions & Answers

How to Convert Strings into Numbers under C-Shell?

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)
Discussion started by: EDALBNUG
8 Replies

10. Shell Programming and Scripting

extracting numbers from strings

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)
Discussion started by: gobi
2 Replies
Login or Register to Ask a Question