Sorting files ending in numbers with "sort"


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting files ending in numbers with "sort"
# 1  
Old 06-03-2013
Sorting files ending in numbers with "sort"

I have a group of files that I need to be sorted by number. I have tried to use the sort command without any luck.
Code:
ls includes* 
includes1
includes10
includes11
includes12
includes2
includes3
includes4
includes5
includes6
includes7
includes8
includes9

I have tried ls includes* |sort -n and I have tried sorting by field. I then tried sort -r to see if the order would reverse. None of these commands changes the order. includes10 includes11 and includes12 should be at the end of these files not at the beginning. Any ideas as to how to accomplish this?
# 2  
Old 06-03-2013
Not very scientific, but:

Code:
$ ls includes* | sort -ts -nsk2
includes1
includes2
includes3
includes4
includes5
includes6
includes7
includes8
includes9
includes10
includes11
includes12

It would be better if the files were created with the numbers padded.
This User Gave Thanks to Scott For This Post:
# 3  
Old 06-03-2013
At first no luck but I got it!

ls includes* | sort -ts -nk2 worked for me. The -nsk2 didn't do a thing. I will have to study the bash man page for sort, but thanks!
# 4  
Old 06-03-2013
Oh yes, that s isn't necessary.
# 5  
Old 06-04-2013
I want to contribute the "normal" sort, too:
Code:
ls includes* | sort -nk1.9

# 6  
Old 06-04-2013
Thank you

Thanks a lot for guiding us here.


Thanks,
R. Singh
# 7  
Old 06-04-2013
Hi.

The non-standard msort utility can be used:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate msort hybrid comparison.

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

# See: http://freecode.com/projects/msort
# Special comparison mode "hybrid".
# Hybrid comparison is like lexicographic comparison except for
# the fact that strings of digits are treated as numbers. 


FILE=${1-data1}

pl " Results of msort hybrid comparison:"
msort -j -q -l -n 1 -c hybrid $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 GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Results of msort hybrid comparison:
includes1
includes2
includes3
includes4
includes5
includes6
includes7
includes8
includes9
includes10
includes11
includes12

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

2. Shell Programming and Scripting

Find out if multiple files have lines ending with"r"

I am trying to find out which files in a group of files have lines ending in r. What I have is this: cat /tmp/*RECORDS| if grep r$>/dev/null; then echo "yes";else echo"no";fi Records is more than one file. There are the following files TEST-RECORDS /volume/testing /volume/programs ... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. UNIX for Dummies Questions & Answers

How to get last word from a line ending with "/" in Unix

Hello All, I have a scenario to read a file containing text like this:(say file name is Dummy.txt) /home/abc/test1/ | file1 /home/abc/test2/ | file2 I used a variable to store the content from file like this (say for line1): File=`head -1 Dummy.txt | cut -f1 -d "|"` Dir=`head -1 Dummy.txt... (2 Replies)
Discussion started by: Quesemail
2 Replies

4. Shell Programming and Scripting

sort, columns, no result! can I print files of "planes"?

hi, please can I ask you for some help? I have data from 3D situation, x y z value I'd like to use gnuplot to generate maps of the value in the planes z=0 to z=1 for example, my file looks like -0,012 0,0060 0,0 0,13972813076023477 -0,012 0,0064319163 4,2894483E-4 ... (1 Reply)
Discussion started by: kocour
1 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

Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column: Total_MB Free_MB OS_MB Name Failgroup Library Label UDID Product Redund Path 1024 851 1024 OCRVOT1_0000 OCRVOT1_0000 System UNKNOWN ... (3 Replies)
Discussion started by: wjssj
3 Replies

7. Shell Programming and Scripting

Set command ending with a "." (dot)

Hi I have a "set" command which ends with a "." (dot), for example: set `grep "\<${pnum}\>" /tstmp/data.txt |sed 's/#//'` . Can somebody help me to understand the purpose of this "set" and "." combination? The problem is that this command does not produce the same result when run on AIX... (2 Replies)
Discussion started by: aoussenko
2 Replies

8. Shell Programming and Scripting

Delete lines ending in "_;" using sed

I could really use some help with this issue. I'm having a lot of trouble getting my sed command to delete only the lines from my file that end with _; I'm also supposed to carry the leading 'c' down to the next line. The commands I've tried either delete everything or nothing at all. Any help... (12 Replies)
Discussion started by: turbulence
12 Replies

9. Shell Programming and Scripting

Sorting problem "sort -k 16,29 sample.txt > output.txt"

Hi all, Iam trying to sort the contents of the file based on the position of the file. Example: $cat sample.txt 0101020060731 ## Header record 1c1 Berger Awc ANP20070201301 4000.50 1c2 Bose W G ANP20070201609 6000.70 1c2 Andy CK ANP20070201230 28000.00... (3 Replies)
Discussion started by: ganapati
3 Replies

10. UNIX for Dummies Questions & Answers

get two strings ending with "." and starting with "."

Hi all, In unix shell, I want to get two strings ending with "." and starting with "." from a string "chan.txt" For example, a string "chan.txt". The first string is "chan" The second string is "txt" Yours Wilson (1 Reply)
Discussion started by: wilsonchan1000
1 Replies
Login or Register to Ask a Question