Sort strings with numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort strings with numbers
# 1  
Old 08-23-2015
Sort strings with numbers

I want to sort my data first by the 2nd field then by the first field.
I can't use
Code:
sort -V

because I don't have gnu sort and cannot install one.
How do I go about this?

Input:

Code:
G456 KT1 34
K234 KT10 45
L2 KT2 26
H5 LAF2 28
F3 LAF2 36

Output:

Code:
G456 KT1 34
L2 KT2 26
K234 KT10 45
F3 LAF2 36
H5 LAF2 28

# 2  
Old 08-23-2015
There are some data specific ways to pre-process your data into fields that a standard sort utility can process, sort it, and then post-process the results to get back your original data in your desired sorted order.

For example, with your sample data (which has single spaces as field separators and the 1st two fields each starting with a string of one or more alphabetic characters followed by a string of one or more decimal digits), you could add spaces before the first digit in the 1st and 2nd fields, sort with options -k3,3 -k4,4n -k1,1 -k2,2n, and then remove the 3rd and 1st spaces from the sorted output.

If your data isn't as simple as shown in your sample (some data in the 1st two fields with no letters, no digits, some numbers with a leading decimal point, numbers containing more then one decimal point, more than one string of letters with numbers interspersed, etc.), then the pre-processing and post-processing steps would be correspondingly more complex.
# 3  
Old 08-24-2015
Without loss of generality (esp. concerning Don Cragun's comments) and taylored to your sample, this would satisfy your request:
Code:
sed -r 's/(^| )([[:alpha:]]+)([[:digit:]]+)/\1\2 \3/g' file | sort -k3,3 -k4,4n -k1,1 -k2,2n | sed -r 's/([[:alpha:]]+) ([[:digit:]]+)/\1\2/g'

# 4  
Old 08-24-2015
Hi.

Utility msort can handle all this internally:
Code:
       msort provides twelve types of key comparison: lexicographic,  numeric,
       numeric  string, hybrid, by string length, by angle, by date, by domain
       name, by time, by ISO8601 date/time stamp, by month name, and random.
-- man msort

Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate ordering of hybrid strings, msort.
# Requires: libc6 (>= 2.7-1), libgmp3c2, libicu38 (>= 3.8-5), libtre4, libuninum5
# If not in repository, see:
# homepage: http://www.billposer.org/Software/msort.html

# 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

FILE=${1-data1}

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

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

pl " Results:"
msort -q -l -n2,2 -chybrid -n1,1 -clexicographic $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39

-----
 Input data file data1:
G456 KT1 34
K234 KT10 45
L2 KT2 26
H5 LAF2 28
F3 LAF2 36

-----
 Expected results:
G456 KT1 34
L2 KT2 26
K234 KT10 45
F3 LAF2 36
H5 LAF2 28

-----
 Results:
G456 KT1 34
L2 KT2 26
K234 KT10 45
F3 LAF2 36
H5 LAF2 28

Best wishes ... cheers, drl
# 5  
Old 08-24-2015
@RudiC, I get this error,
Code:
sed: illegal option -- r

, can awk be used?
# 6  
Old 08-24-2015
Quote:
Originally Posted by aydj
@RudiC, I get this error,
Code:
sed: illegal option -- r

, can awk be used?
What operating system are you using? It is a good idea to always give us this information (and the shell you're using) when you ask for help so we can choose utilities and options that will work in your environment when we make suggestions.
# 7  
Old 08-24-2015
OS = Solaris 10
Shell = ksh
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 containing numbers

How can I sort this, first by 2nd field then by 1st field. tried sort -b -k 2,2 Input: AS11 AB1 BD34 AB10 AF12 AC2 A345 AB10 R134 AB2 456 AC10 TTT2 BD12 desired output: AS11 AB1 R134 AB2 A345 AB10 BD34 AB10 AF12 AC2 456 AC10 TTT2 BD12 (2 Replies)
Discussion started by: aydj
2 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