Sorting alphanumeric strings without a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting alphanumeric strings without a pattern
# 8  
Old 06-05-2012
Code:
# awk '{s=gensub("[^0-9]*([0-9][0-9]*).*","\\1",$0);print s,$0}' infile|sort -n|awk '{print $2}'

# 9  
Old 06-05-2012
@ygemici, that will not work for file names with spaces and/or with multiple number sequences in them... gensub is gawk only..

Last edited by Scrutinizer; 06-05-2012 at 08:41 AM..
# 10  
Old 06-05-2012
Quote:
Originally Posted by Scrutinizer
@ygemici, that will not work for file names with spaces and/or with multiple number sequences in them... gensub is gawk only..
Code:
# awk '{x=$0;gsub("[^0-9]"," ");print $NF,x}' infile|sort -n|awk '{$1="";sub(" ","")}1'

# 11  
Old 06-05-2012
Yup that would not have those shortcomings I think Smilie
# 12  
Old 06-05-2012
Quote:
Originally Posted by Scrutinizer
Yup that would not have those shortcomings I think Smilie
Smilie Smilie
# 13  
Old 06-22-2012
Thanks again for your help!!!

I tried with severl filenames but I encountered one problem!

With these files:


03_000a.jpg
03_000b.jpg
03_000c.jpg
03_001.png
03_002.png
03_003.png
03_004-005.png
03_006.png
03_007.png
03_008.png
03_009.png
03_010.png
Credits10.png
Credits11.png



I get this sorting:

03_000a.jpg
03_000b.jpg
03_000c.jpg
03_001.png
03_002.png
03_003.png
03_004-005.png
03_006.png
03_007.png
03_008.png
03_009.png
03_010.png
Credits10.png
03_011.png
Credits11.png
03_012.png
03_013.png

Because of this problem (first numbers are the sorting order).

000 03_000a.jpg
000 03_000b.jpg
000 03_000c.jpg
001 03_001.png
002 03_002.png
003 03_003.png
005 03_004-005.png
006 03_006.png
007 03_007.png
008 03_008.png
009 03_009.png
010 03_010.png
011 03_011.png
012 03_012.png
013 03_013.png
10 Credits10.png
11 Credits11.png

I think the problem is that filenames aren't consistent in this folder.
Is it possible to reproduce the "sort by name" windows option?

Thanks again!!



EDIT: probably here is a solution:

1) get a column of every first character using
Code:
sed 's/\(.\{1\}\).*/\1/' file1

2) get a column of incremental numbers just like I already do using
Code:
sed 's/.*[^0-9]\([0-9][0-9]*\)[^0-9]*$/\1 &/' file2

(although I need to get only the first column)
3) sort the lines using two keys: first one should be file1, second one should be file2

this way, I can sort twice, once alphabetically, then, keeping the first sorting result, using incremental numbers from files name...
I think this should work, but I don't know how to do it...

Last edited by silver18; 06-22-2012 at 01:38 PM..
# 14  
Old 06-22-2012
Do you know perl...because with perl you can split and store filenames in a hash array and sort them using either the key which can be the name...or the value which would be the numeric part of the filename or on both the key and value...so you have a lot of options to play with in perl...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Include strings between a pattern

Hi, I have two files: File1: sample statements1 <UID = " " PWD = " "> sample statements2 File2: UID ="admin" PWD ="password" (6 Replies)
Discussion started by: manid
6 Replies

2. Shell Programming and Scripting

Sorting content between match pattern and move on with awk and sed

S 0.0 0.0 (reg, inst050) k e f d c S 0.0 0.0 (mux, m030) k g r s x v S 0.0 0.0 (reg, inst020) q s n m (12 Replies)
Discussion started by: ctphua
12 Replies

3. UNIX for Dummies Questions & Answers

Want to sort a file which contains alphanumeric strings

I want to sort a file which contains alphanumeric string. bash-3.00$ cat abc mtng1so mtng2so mtng11so mtng9so mtng23so mtng7so hstg2so hstg9so hstg1so hstg11so hstg13so bash-3.00$ Want output like this, using one liner. hstg1so (1 Reply)
Discussion started by: Raza Ali
1 Replies

4. Shell Programming and Scripting

Sorting block of lines based on specified strings

Below is the part of contents of a file cat outputFile.txt :APP-MS-service_monitoring-118054-DiskFilling-S Description=Disk is getting filled. If Condition=If Event/Log Level(ANY) and matching substring &quot;DiskFilling&quot; Log: xx.x.x..xx.: 4 days 1:33:54.42 Date and time=03-18-2013... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

6. UNIX for Dummies Questions & Answers

Sorting of alphanumeric filenames..

Hi All, I want sort following files in ascending order. exp_installer_CC4160-file10.dmp exp_installer_CC4160-file11.dmp exp_installer_CC4160-file12.dmp exp_installer_CC4160-file13.dmp... (3 Replies)
Discussion started by: gramakrishna
3 Replies

7. Programming

Sorting a vector of strings into numerical order.

I have a vector of strings that contain a list of channels like this: 101,99,22HD,432,300HD I have tried using the sort routine like this: sort(mychans.begin(),mychans.end()); For some reason my channels are not being sorted at all. I was hoping someone might have some input that might... (2 Replies)
Discussion started by: sepoto
2 Replies

8. Shell Programming and Scripting

Sorting with non- and alphanumeric characters

Hi guys, I'm new to this forum and I'm not a UNIX expert. I can't figure out this certain problem i'm having: I need to sort some words, some of the words are annotations (enclosed within < and >). I need to have them sorted alphabetically with all non-alphanumeric characters up front. For... (2 Replies)
Discussion started by: fed.m.ang
2 Replies

9. UNIX for Advanced & Expert Users

alphanumeric Sorting

Hi , I have a requirement where one column have to be sorted (delimiter is pipe) for eg: My input filed is as below 1|FIAT|0010103|23011|01/01/2000|31/12/9999|1.15 2|232|613|1 2|234|743|1 2|234|793|1 2|234|893|1 1|FIAT|0010103|23012|01/01/2000|31/12/9999|1.15 2|230|006|0 2|230|106|0... (9 Replies)
Discussion started by: laxmi131
9 Replies

10. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies
Login or Register to Ask a Question