Sort command, more than one delimiter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort command, more than one delimiter
# 1  
Old 03-02-2015
Sort command, more than one delimiter

I have the following file:
Code:
/vol/labor_vol1
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor_vol2
/vol/labor_vol22
/vol/labor_vol3
/vol/labor_house
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

I want to see it sorted like this:

Code:
/vol/labor_vol1
/vol/labor_vol2
/vol/labor_vol3
/vol/labor_vol4
/vol/labor_vol5
/vol/labor_vol6
/vol/labor_vol7
/vol/labor_vol8
/vol/labor_vol9
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor_vol22
/vol/labor_house
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

Is there a way to do this with sort?
I've tried:
Code:
sort -t '/' -k3.6n,3.10n

and similar combinations without luck
# 2  
Old 03-02-2015
This is as close as I can get...(AIX is my O/S)...
Code:
[josephgr@redhat ~]$ cat x.x
/vol/labor_vol1
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor_vol2
/vol/labor_vol22
/vol/labor_vol3
/vol/labor_house
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

Code:
[josephgr@redhat ~]$ sort -V -t'_' -k2,2  x.x
/vol/labor_house
/vol/labor_vol1
/vol/labor_vol2
/vol/labor_vol3
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor_vol22
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

Oops, just saw you have both labor and labor1
# 3  
Old 03-02-2015
The standard sort utility isn't able to perform a numeric sort on numbers at the end of a field when the string of characters before the numbers are variable length. And that is a very strange sort order (with house sorting after vol). And I don't understand why the following lines which are not in your input file:
Code:
/vol/labor_vol4
/vol/labor_vol5
/vol/labor_vol6
/vol/labor_vol7
/vol/labor_vol8
/vol/labor_vol9

are expected to magically appear in the output.

If you're willing to only output data that appears in your input file, we can sort in increasing order on the characters before the underscore, decreasing order on the characters after the underscore up to (but not including) any digits at the end, and then by increasing numeric order on any digits at the end using:
Code:
sed -e 's/[[:digit:]]*$/ &/' -e 's/_/ /' file > file.$$
sort -k1,1 -k2,2r -k3,3n -o file.$$ file.$$
sed -e 's/ /_/' -e 's/ //' file.$$
rm file.$$

I'm using sed to pre-process your input to create three sort fields with <space> as the field delimiter, sort to sort the file (now with a constant field delimiter), and sed again to replace the removed underscore and get rid of the inserted <space>s. With your sample input file, this produces the output:
Code:
/vol/labor_vol1
/vol/labor_vol2
/vol/labor_vol3
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor_vol22
/vol/labor_house
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

# 4  
Old 03-02-2015
Hi.

The code msort allows keys of the mode hybrid lexicographic-numeric comparison (for things like filenames and section headings, so that, e.g., 2a will precede 10b).

I'll assume that the house datum is an anomaly, and the sort would look like this:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate ordering with msort, especially hybrid keys.
# For msort not in a repository, see:
# http://freecode.com/projects/msort

# 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 msort

FILE=${1-data1}

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

pl " Results:"
msort -l -d'_' -j -q -n1 -c"lexicographic" -n2 -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 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
/vol/labor_vol1
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor_vol2
/vol/labor_vol22
/vol/labor_vol3
/vol/labor_house
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

-----
 Results:
/vol/labor_house
/vol/labor_vol1
/vol/labor_vol2
/vol/labor_vol3
/vol/labor_vol10
/vol/labor_vol11
/vol/labor_vol12
/vol/labor_vol13
/vol/labor_vol14
/vol/labor_vol15
/vol/labor_vol16
/vol/labor_vol17
/vol/labor_vol18
/vol/labor_vol19
/vol/labor_vol22
/vol/labor1_vol20
/vol/labor1_vol21
/vol/labor1_vol23
/vol/labor1_vol24
/vol/labor1_vol25
/vol/labor1_vol26
/vol/labor1_vol27

See also https://www.unix.com/man-page/debian/1/msort/

Best wishes ... cheers, drl

Last edited by drl; 03-02-2015 at 10:30 PM.. Reason: Add man page reference
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

3. UNIX for Dummies Questions & Answers

set output delimiter as tab in cut command

I can not make it work, it prints \t rather than introduce tabs. cut -d "," -f 4,8 Samples.csv --output-delimiter="\t" | sort > out Since I am running this command within a shell script, I tried manually inserting tab in this command, still does not work. I am using bash shell Suggestions... (8 Replies)
Discussion started by: analyst
8 Replies

4. Shell Programming and Scripting

sort text having delimiter with "|" (pipe)

i am having text file below NARGU S S 12358 SALES REP |22| Acccount/s RAJU R B 64253 SALES REP |12| Acccount/s RUKMAN S 32588 SALES REP |10| Acccount/s NARGUND S S 12356... (3 Replies)
Discussion started by: suryanarayana
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

CUT command delimiter in reverse

Hi, I've a situation where, a=xxx.yyy.zzz.txt EXTN=`echo $a | cut -d . -f2` Using the above code it delimites and will return "yyy.zzz.txt" to EXTN. But i need to get only the extension "txt". so as per the above code it delimits in the first "." itself. Can anyone help how to do... (6 Replies)
Discussion started by: skcvasanth
6 Replies

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

8. UNIX for Advanced & Expert Users

How can I use double character delimiter in the cut command

Hi All, Can the cut command have double character delimiter? If yes, how can we use it. if my data file contains : apple || mango || grapes i used cut -f1 -d"||" filename but got an error. Plz help.... Thanks. (1 Reply)
Discussion started by: AshishK
1 Replies

9. Shell Programming and Scripting

sort command delimiter

Hello All, I'm trying to use the sort command on a text file, the delimiter used in this file is a non-prining character '\027' I tried to use the command -t option but it's not really working. sort +n4 -t'\027' filename <<-- this is what I was trying and not working. Your help is... (1 Reply)
Discussion started by: Nomaad
1 Replies

10. Shell Programming and Scripting

Dynamic delimiter in cut command

hello.. my prob is as follows: i have to read from a file which may have different formats depending upon the delimiter used in the data of the file.now i need that the user input the delimiter.the input delimiter is stored in a variable and is used on cut command to retrieve necessary... (3 Replies)
Discussion started by: tej.buch
3 Replies
Login or Register to Ask a Question