Sponsored Content
Top Forums Shell Programming and Scripting sort based on the own pattern Post 302431195 by pludi on Monday 21st of June 2010 06:10:50 AM
Old 06-21-2010
What about d-z, A-Z, 0-9, ...? What about values with more than 1 character?

One possible approach, if it's only a few possible characters (=< 10), would be to translate them to numbers, do a numerical comparison, and translate them back after sorting, eg:
Code:
#!/usr/bin/perl
@test = qw/a b c d/;
print "@test\n";
@sorted = map { $_->[0] }
  sort { $a->[1] cmp $b->[1] }
  map { $a = $_; $a =~ tr/abcd/4213/; [ $_, $a ] } @test;
print "@sorted\n";

This User Gave Thanks to pludi For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sort by based on multiple columns

Hi, Is there any way to sort a file in cshell by sort command, sorting it by multiple fields, like to sort it first by the second column and then by the first column. Thanks forhead (1 Reply)
Discussion started by: Takeeshe
1 Replies

2. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

3. Shell Programming and Scripting

Sort based on filenames

Hi All, When i give ls -ltr my filenames looks like this: Filename Pattern: Abc_Def_mmddyyyyHHmm.csv $ ls -ltr Jun 05 04:30 Abc_Def_060520111245.csv Jun 05 08:40 Abc_Def_071220121458.csv Jun 06 03:30 Abc_Def_071220111458.csv Jun 06 04:25 Abc_Def_060620110439.csv Jun 07 04:37... (12 Replies)
Discussion started by: HemaV
12 Replies

4. Shell Programming and Scripting

Sort and extract based on two files

Hi, I am having trouble sorting one file based on another file. I tried the grep -f function and failed. Basically what I have is two files that look like this: File 1 (the list) gh aba for hmm File 2 ( the file that needs to be sorted) aba 2 4 6 7 for 2 4 7 4... (4 Replies)
Discussion started by: phil_heath
4 Replies

5. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

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

7. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

8. Shell Programming and Scripting

Sort based on certain value in a column

Hi, i need to sort content of files based on a specific value. An example as below. Input1.txt Col_1 SW_MH2_ST ST_F72_9S SW_MH3_S6 Col_2 SW_MH3_AS7 ST_S15_9CH SW_MH3_AS8 SW_MH3_ST Col_3 ST_M93_SZ ST_C16_TC (12 Replies)
Discussion started by: redse171
12 Replies

9. UNIX for Beginners Questions & Answers

Any way to sort ps output based on STIME?

Hi, This is one of the thing that am looking for when I post the question on the ps wrapper. It has since been closed as it has taken me too long to post an example. I have replaced some of the original content of the ps output. uname -a = SunOS <hostname> 5.11 11.3 sun4v sparc sun4v ... (1 Reply)
Discussion started by: newbie_01
1 Replies

10. UNIX for Beginners Questions & Answers

Sort based on one column

Hi All , I am having an input file like this Input file 7 sks/jsjssj/ddjd/hjdjd/hdhd/Q 10 0.5 13 dkdkd/djdjd/djdjd/djd/QB 01 0.5 ldld/dkd/jdf/fjfjf/fjf/Q 0.5 10 sjs/jsdd/djdkd/dhd/Q 01 0.5 21 kdkd/djdd/djdd/jdd/djd/QB 01 0.5 dkdld/djdjd/djd/Q 01 0.5 ... (9 Replies)
Discussion started by: kshitij
9 Replies
Sort::Naturally(3pm)					User Contributed Perl Documentation				      Sort::Naturally(3pm)

NAME
Sort::Naturally -- sort lexically, but sort numeral parts numerically SYNOPSIS
@them = nsort(qw( foo12a foo12z foo13a foo 14 9x foo12 fooa foolio Foolio Foo12a )); print join(' ', @them), " "; Prints: 9x 14 foo fooa foolio Foolio foo12 foo12a Foo12a foo12z foo13a (Or "foo12a" + "Foo12a" and "foolio" + "Foolio" and might be switched, depending on your locale.) DESCRIPTION
This module exports two functions, "nsort" and "ncmp"; they are used in implementing my idea of a "natural sorting" algorithm. Under natural sorting, numeric substrings are compared numerically, and other word-characters are compared lexically. This is the way I define natural sorting: o Non-numeric word-character substrings are sorted lexically, case-insensitively: "Foo" comes between "fish" and "fowl". o Numeric substrings are sorted numerically: "100" comes after "20", not before. o W substrings (neither words-characters nor digits) are ignored. o Our use of w, d, D, and W is locale-sensitive: Sort::Naturally uses a "use locale" statement. o When comparing two strings, where a numeric substring in one place is not up against a numeric substring in another, the non-numeric always comes first. This is fudged by reading pretending that the lack of a number substring has the value -1, like so: foo => "foo", -1 foobar => "foo", -1, "bar" foo13 => "foo", 13, foo13xyz => "foo", 13, "xyz" That's so that "foo" will come before "foo13", which will come before "foobar". o The start of a string is exceptional: leading non-W (non-word, non-digit) components are are ignored, and numbers come before letters. o I define "numeric substring" just as sequences matching m/d+/ -- scientific notation, commas, decimals, etc., are not seen. If your data has thousands separators in numbers ("20,000 Leagues Under The Sea" or "20.000 lieues sous les mers"), consider stripping them before feeding them to "nsort" or "ncmp". The nsort function This function takes a list of strings, and returns a copy of the list, sorted. This is what most people will want to use: @stuff = nsort(...list...); When nsort needs to compare non-numeric substrings, it uses Perl's "lc" function in scope of a <use locale>. And when nsort needs to lowercase things, it uses Perl's "lc" function in scope of a <use locale>. If you want nsort to use other functions instead, you can specify them in an arrayref as the first argument to nsort: @stuff = nsort( [ &string_comparator, # optional &lowercaser_function # optional ], ...list... ); If you want to specify a string comparator but no lowercaser, then the options list is "[&comparator, '']" or "[&comparator]". If you want to specify no string comparator but a lowercaser, then the options list is "['', &lowercaser]". Any comparator you specify is called as "$comparator->($left, $right)", and, like a normal Perl "cmp" replacement, must return -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument. Any lowercaser function you specify is called as "$lowercased = $lowercaser->($original)". The routine must not modify its $_[0]. The ncmp function Often, when sorting non-string values like this: @objects_sorted = sort { $a->tag cmp $b->tag } @objects; ...or even in a Schwartzian transform, like this: @strings = map $_->[0] sort { $a->[1] cmp $b->[1] } map { [$_, make_a_sort_key_from($_) ] @_ ; ...you wight want something that replaces not "sort", but "cmp". That's what Sort::Naturally's "ncmp" function is for. Call it with the syntax "ncmp($left,$right)" instead of "$left cmp $right", but otherwise it's a fine replacement: @objects_sorted = sort { ncmp($a->tag,$b->tag) } @objects; @strings = map $_->[0] sort { ncmp($a->[1], $b->[1]) } map { [$_, make_a_sort_key_from($_) ] @_ ; Just as with "nsort" can take different a string-comparator and/or lowercaser, you can do the same with "ncmp", by passing an arrayref as the first argument: ncmp( [ &string_comparator, # optional &lowercaser_function # optional ], $left, $right ) You might get string comparators from Sort::ArbBiLex. NOTES
o This module is not a substitute for Sort::Versions! If you just need proper version sorting, use that! o If you need something that works sort of like this module's functions, but not quite the same, consider scouting thru this module's source code, and adapting what you see. Besides the functions that actually compile in this module, after the POD, there's several alternate attempts of mine at natural sorting routines, which are not compiled as part of the module, but which you might find useful. They should all be working implementations of slightly different algorithms (all of them based on Martin Pool's "nsort") which I eventually discarded in favor of my algorithm. If you are having to naturally-sort very large data sets, and sorting is getting ridiculously slow, you might consider trying one of those discarded functions -- I have a feeling they might be faster on large data sets. Benchmark them on your data and see. (Unless you need the speed, don't bother. Hint: substitute "sort" for "nsort" in your code, and unless your program speeds up drastically, it's not the sorting that's slowing things down. But if it is "nsort" that's slowing things down, consider just: if(@set >= SOME_VERY_BIG_NUMBER) { no locale; # vroom vroom @sorted = sort(@set); # feh, good enough } elsif(@set >= SOME_BIG_NUMBER) { use locale; @sorted = sort(@set); # feh, good enough } else { # but keep it pretty for normal cases @sorted = nsort(@set); } o If you do adapt the routines in this module, email me; I'd just be interested in hearing about it. o Thanks to the EFNet #perl people for encouraging this module, especially magister and a-mused. COPYRIGHT AND DISCLAIMER
Copyright 2001, Sean M. Burke "sburke@cpan.org", all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. AUTHOR
Sean M. Burke "sburke@cpan.org" perl v5.10.0 2004-12-30 Sort::Naturally(3pm)
All times are GMT -4. The time now is 01:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy