Sponsored Content
Top Forums Shell Programming and Scripting sort based on the own pattern Post 302431291 by jimmy_y on Monday 21st of June 2010 10:56:20 AM
Old 06-21-2010
hi pludi,

i tried on sort and map, tried to want to replace each element with number, but failed. I used one way which is very unefficient to do it.

Code:
[root@P1Portal ]# cat 1.pl
#!/usr/bin/perl
use strict;
use warnings;

my @test = qw/aaa bb 44 BB9/;
my @new;
foreach (@test) {
        if ($_ =~ m/^(BB9)/i ) {
                $new[0] = $_;
        } elsif ($_ =~ m/^(bb)/i ) {
                $new[1] = $_;
        } elsif ($_ =~ m/^(aaa)/i ) {
                $new[2] = $_;
        } elsif ($_ =~ m/^(44)/i ) {
                $new[3] = $_;
        }
}
print @new;

exit;
#my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { $a = $_; $a = splice(@test,0,4,'4','3','2','1'); [ $_, $a ] } @test;
#print "@sorted\n";

[root@P1Portal ]# ./1.pl
BB9bbaaa44

Smilie
 

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
CORE(3pm)						 Perl Programmers Reference Guide						 CORE(3pm)

NAME
CORE - Namespace for Perl's core routines SYNOPSIS
BEGIN { *CORE::GLOBAL::hex = sub { 1; }; } print hex("0x50")," "; # prints 1 print CORE::hex("0x50")," "; # prints 80 CORE::say "yes"; # prints yes BEGIN { *shove = &CORE::push; } shove @array, 1,2,3; # pushes on to @array DESCRIPTION
The "CORE" namespace gives access to the original built-in functions of Perl. The "CORE" package is built into Perl, and therefore you do not need to use or require a hypothetical "CORE" module prior to accessing routines in this namespace. A list of the built-in functions in Perl can be found in perlfunc. For all Perl keywords, a "CORE::" prefix will force the built-in function to be used, even if it has been overridden or would normally require the feature pragma. Despite appearances, this has nothing to do with the CORE package, but is part of Perl's syntax. For many Perl functions, the CORE package contains real subroutines. This feature is new in Perl 5.16. You can take references to these and make aliases. However, some can only be called as barewords; i.e., you cannot use ampersand syntax (&foo) or call them through references. See the "shove" example above. These subroutines exist for all keywords except the following: "__DATA__", "__END__", "and", "cmp", "default", "do", "dump", "else", "elsif", "eq", "eval", "for", "foreach", "format", "ge", "given", "goto", "grep", "gt", "if", "last", "le", "local", "lt", "m", "map", "my", "ne", "next", "no", "or", "our", "package", "print", "printf", "q", "qq", "qr", "qw", "qx", "redo", "require", "return", "s", "say", "sort", "state", "sub", "tr", "unless", "until", "use", "when", "while", "x", "xor", "y" Calling with ampersand syntax and through references does not work for the following functions, as they have special syntax that cannot always be translated into a simple list (e.g., "eof" vs "eof()"): "chdir", "chomp", "chop", "defined", "delete", "each", "eof", "exec", "exists", "keys", "lstat", "pop", "push", "shift", "splice", "split", "stat", "system", "truncate", "unlink", "unshift", "values" OVERRIDING CORE FUNCTIONS
To override a Perl built-in routine with your own version, you need to import it at compile-time. This can be conveniently achieved with the "subs" pragma. This will affect only the package in which you've imported the said subroutine: use subs 'chdir'; sub chdir { ... } chdir $somewhere; To override a built-in globally (that is, in all namespaces), you need to import your function into the "CORE::GLOBAL" pseudo-namespace at compile time: BEGIN { *CORE::GLOBAL::hex = sub { # ... your code here }; } The new routine will be called whenever a built-in function is called without a qualifying package: print hex("0x50")," "; # prints 1 In both cases, if you want access to the original, unaltered routine, use the "CORE::" prefix: print CORE::hex("0x50")," "; # prints 80 AUTHOR
This documentation provided by Tels <nospam-abuse@bloodgate.com> 2007. SEE ALSO
perlsub, perlfunc. perl v5.18.2 2013-11-04 CORE(3pm)
All times are GMT -4. The time now is 02:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy