Sort strings with numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort strings with numbers
# 8  
Old 08-24-2015
With awk you could try:
Code:
nawk '{p=$0;$1=$2 FS $1; gsub(/[0-9]+/," &",$1)}{print $1,p}' file | sort -n | nawk '{print $5,$6,$7}'

# 9  
Old 08-25-2015
I don't have access to a solaris system. Does it's sed support the -E option (which is equivalent to -r)?
# 10  
Old 08-25-2015
@Scrutinizer, seem not to be working, This is the output I got:
Code:
G456 KT1 34
K234 KT10 45
L2 KT2 26
F3 LAF2 36
H5 LAF2 28

---------- Post updated at 10:25 ---------- Previous update was at 10:22 ----------

Quote:
Originally Posted by RudiC
I don't have access to a solaris system. Does it's sed support the -E option (which is equivalent to -r)?
No -E option:

Code:
sed: illegal option -- E
sed: illegal option -- E

# 11  
Old 08-25-2015
try
Code:
sed  's/\(^\| \)\([[:alpha:]]\+\)\([[:digit:]]\+\)/\1\2 \3/g' file | sort -k3,3 -k4,4n -k1,1 -k2,2n | sed 's/\([[:alpha:]]\+\) \([[:digit:]]\+\)/\1\2/g'

# 12  
Old 08-25-2015
Quote:
Originally Posted by RudiC
try
Code:
sed  's/\(^\| \)\([[:alpha:]]\+\)\([[:digit:]]\+\)/\1\2 \3/g' file | sort -k3,3 -k4,4n -k1,1 -k2,2n | sed 's/\([[:alpha:]]\+\) \([[:digit:]]\+\)/\1\2/g'

Not working, this is the output:
Code:
L2 KT2 26
H5 LAF2 28
G456 KT1 34
F3 LAF2 36
K234 KT10 45

# 13  
Old 08-25-2015
This is becoming a bit confusing. Try
Code:
sed  's/\([[:alpha:]][[:alpha:]]*\)\([[:digit:]][[:digit:]]*\)/\1 \2/g' file | sort -k3,3 -k4,4n -k1,1 -k2,2n | sed 's/\([[:alpha:]][[:alpha:]]*\) \([[:digit:]][[:digit:]]*\)/\1\2/g'

# 14  
Old 08-25-2015
Quote:
Originally Posted by aydj
@Scrutinizer, seem not to be working, This is the output I got:
Code:
G456 KT1 34
K234 KT10 45
L2 KT2 26
F3 LAF2 36
H5 LAF2 28

[..]
Yes, the sort wasn't right. Try:
Code:
nawk '{p=$0;$1=$2 FS $1; gsub(/[0-9]+/," &",$1)}{print $1,p}' file | sort -k1,1 -k2,2n -k3,3 -k4,4n | nawk '{print $5,$6,$7}'



---
Quote:
Originally Posted by RudiC
This is becoming a bit confusing. Try
Code:
sed  's/\([[:alpha:]][[:alpha:]]*\)\([[:digit:]][[:digit:]]*\)/\1 \2/g' file | sort -k3,3 -k4,4n -k1,1 -k2,2n | sed 's/\([[:alpha:]][[:alpha:]]*\) \([[:digit:]][[:digit:]]*\)/\1\2/g'

On Solaris one needs to use /usr/xpg4/bin/sed to use POSIX character classes..

Last edited by Scrutinizer; 08-25-2015 at 01:38 PM..
This User Gave Thanks to Scrutinizer 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 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