Sort only numbers within a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort only numbers within a string
# 1  
Old 02-01-2019
Sort only numbers within a string

Hi,

I am having contents in a file like below,

Code:
cat testfile
rpool/swap
rpool/swap14
rpool/swap2
rpool/swap3

I want to sort the above contents like,
Code:
rpool/swap
rpool/swap2
rpool/swap3
rpool/swap14

I have tried in this way,

Code:
sort  -n -k2 testfile

Where am i doing wrong?

OS:Solaris11x86

TIA
# 2  
Old 02-01-2019
Code:
sort -t/ -k2.5n testfile

SunOS sol 11.4
# 3  
Old 02-01-2019
The sort fields are "blank"-delimited by default.
You would need a preformatting like
Code:
sed 's/[0-9]\{1,\}/ &/g' testfile | sort -n -k2

Without a certain delimiter it becomes difficult. An attempt to undo the space delimiter:
Code:
sed 's/[0-9]\{1,\}/ &/g' testfile | sort -n -k2 | sed 's/ \([0-9]\)/\1/g'

# 4  
Old 02-02-2019
Hi.

This script uses a sort perl code that in turn uses a perl module Sort::Naturally. That module was not available in my edition of Solaris, but it may be in yours. I copied it from the cpan repository. Note that the guts of the solution is simply calling the perl code with the filename, most of the remainder of the script is supporting code to show data, results, verification, etc. I have added a few lines to illustrate the generality.

Additional information can be found at:
sorting - Perl sort numbers naturally - Stack Overflow
[ Natural Sort ]: Sorting a string with numbers

Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate sorting with embedded numeric strings.

# 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 "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl dixf

FILE=${1-data2}
N=${FILE//[A-Za-z]/}
E=expected-output$N

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

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

pl " Expected output:"
cat $E

pl " Basic perl code:"
cat p3

pl " Results:"
./p3 $FILE |
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C f1 "$E" || ( pe; pe " Results cannot be verified." ) >&2

pl " Details of p3:"
dixf ./p3

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86
bash GNU bash 4.1.17
perl 5.12.5
dixf (local) 1.54

-----
 Input data file data2:
rpool/swap
rpool/swap14
rpool/swap2
rpool/swap3
a3
a1
a20
a2
a
b25
b2a1
b2a3
b2a2
b1a1

-----
 Expected output:
a
a1
a2
a3
a20
b1a1
b2a1
b2a2
b2a3
b25
rpool/swap
rpool/swap2
rpool/swap3
rpool/swap14

-----
 Basic perl code:
#!/usr/bin/env perl

# @(#) p3       Demonstrate "Natural" sort perl module.

use Sort::Naturally;

my(@data) = <>;
my @sorted = nsort(@data);
print @sorted;

exit(0);

-----
 Results:
a
a1
a2
a3
a20
b1a1
b2a1
b2a2
b2a3
b25
rpool/swap
rpool/swap2
rpool/swap3
rpool/swap14

-----
 Verify results if possible:

-----
 Comparison of 14 created lines with 14 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output2 have same content.

-----
 Details of p3:
p3      Demonstrate "Natural" sort perl module. (what)
Path    : ./p3
Length  : 11 lines
Type    : executable /usr/bin/env script
Shebang : #!/usr/bin/env perl
Modules : (for perl codes)
 Sort::Naturally        1.03

Best wishes ... cheers, drl
These 4 Users Gave Thanks to drl For This Post:
# 5  
Old 02-02-2019
If you can make GNU sort available on Solaris:
Code:
sort -V file

Regular sort, for this particular input try:
Code:
sort -k1,1.10 -k1.11n


Last edited by Scrutinizer; 02-03-2019 at 02:34 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-02-2019
It was not easy to figure it out. Just in case I will leave it here. Thanks
Install Perl Module #sudo make install
module Sort::Naturally

Last edited by nezabudka; 02-02-2019 at 03:02 PM..
This User Gave Thanks to nezabudka For This Post:
# 7  
Old 02-02-2019
just a pathetic attempt Smilie
Code:
cat prog.sh

#!/bin/bash
file=testfile
echo ------------------
cat $file
while read -r d; do
        echo ${d//[^0-9]} ${d//[^[:alpha:]]} $d
done < $file | sort -k2.1,2.2d -k1,1n | sed 's/.* //'

./prog.sh
rpool/swap
rpool/swap14
rpool/swap2
rpool/swap3
a3
a1
a20
a2
a
b25
b2a1
b2a3
b2a2
b1a1
------------------
a
a1
a2
a3
a20
b1a1
b2a1
b2a2
b2a3
b25
rpool/swap
rpool/swap2
rpool/swap3
rpool/swap14

This User Gave Thanks to nezabudka For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort based on numbers

I have a file which has the following data :- how can I sort the data in descending order . My files may have the first column with 1 to 10000 numbers .I need to arrange them in descending order . Thanks (2 Replies)
Discussion started by: lazydev
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

Sort numbers which has colon (:) in between

Although i tried multiple option i couldn't find a way to get the rigt ouput. Say i have the following data cat file.txt C request C response C request C response The output should look like (9 Replies)
Discussion started by: varu0612
9 Replies

4. Shell Programming and Scripting

How to sort version numbers?

I would like to know how to sort version numbers, using bash or perl. I would like to sort file names that are program names with version numbers and extensions, such as hello-0.2.3.tar.gz and hello-0.10.3.tar.gz. Version numbers of computer programs do not comply with the mathematical rule... (3 Replies)
Discussion started by: LessNux
3 Replies

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

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

7. UNIX for Dummies Questions & Answers

sorting numbers with sort -n

Looking for help for sort, I learned that for sorting numbers I use: sort -n but it seems that that is not enough when you have numbers like 0.2000E+7 for example, sort -n will not worry about the E+7 part, and will just sort the numbers like 0.2000. Exapmle: cat example.txt .91000E+07... (9 Replies)
Discussion started by: cosmologist
9 Replies

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

9. Shell Programming and Scripting

Sort by numbers, then alphabetically

Hey guys, I have a file that contains the following: 366 K 364 Q 12 UB 7 INC. P 4 Law 2 LAMB 2 High 1 QEG 1 OF 1 LC 1 B As you can see, it's already sorted by numerical order, how do I sort it again, breaking the ties by using the alphabetical order of the second column, but... (2 Replies)
Discussion started by: Andrew9191
2 Replies

10. Shell Programming and Scripting

How to sort a string with numbers

Hi, I need help to sort a file contents. I am using sort -r option to basically reverse the comparison in descending order. However, i found out that my file is not sorted according, can anyone please help. My data is something like:- Hello world 20.982342864 343 19.234355545 222... (5 Replies)
Discussion started by: ahjiefreak
5 Replies
Login or Register to Ask a Question