Perl sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl sorting
# 1  
Old 03-15-2010
Perl sorting

Hi,
I have a file in this format:
a b c d e
a b c d e
a b c d e

i need perl script to sort 2nd column in alphabatical order

The script i use is
Code:
#!/usr/bin/perl

my @words=<>;



foreach(sort mysort @words) {

 print;

}



sub mysort {



 lc($a) cmp lc($b);



}


This script sort the first column. What should i do to sort the second column.

Thanks
# 2  
Old 03-15-2010
MySQL

You can use the following command for sorting the data by second column.

Code:
sort -t ' ' -k 2 Data.txt

Input
a b c
c b s
a a c
a c e
f f d
c d g

Output
a a c
a b c
c b s
a c e
c d g
f f d
# 3  
Old 03-15-2010
is this homework?
you need to split into columns if you don't want to use
the first reply to your thread.
i.e. you need to extract the second column from $a and $b before you cmp
# 4  
Old 03-15-2010
Thanks for reply,
I know how to do with sort -k2 file but i want to try with perl
Quote:
Originally Posted by ungalnanban
You can use the following command for sorting the data by second column.

Code:
sort -t ' ' -k 2 Data.txt

Input
a b c
c b s
a a c
a c e
f f d
c d g

Output
a a c
a b c
c b s
a c e
c d g
f f d


---------- Post updated at 11:39 AM ---------- Previous update was at 11:38 AM ----------

No not homework. Trying to learn different things in unix

i will try split and let u know in 4 hrs if that work or not

Thanks for reply
Quote:
Originally Posted by bigearsbilly
is this homework?
you need to split into columns if you don't want to use
the first reply to your thread.
i.e. you need to extract the second column from $a and $b before you cmp
# 5  
Old 03-22-2010
summink like:


Code:
 

cat:$perl -e 'print sort {(split " ", $a)[4] <=> (split " ", $b)[4]} (qx/ls -l/)' 
total 52996
-rw-r--r--  1 michalejackson michalejackson        0 Feb 16 12:39 a2_FP4Hzh
-rw-r--r--  1 michalejackson michalejackson        0 Feb 16 09:47 a2_MbRNml
-rw-r--r--  1 michalejackson michalejackson        0 Feb 16 12:39 a2_eJXzSY
-rw-rw-r--  1 nagios      nagios             0 Feb  8 16:29 service_perfdata
-rw-r--r--  1 nagios      nagios            98 Mar 22 10:01 wrapper.log
-rw-------  1 root        root             622 Mar 17 15:00 etherXXXXkGKzPg
drwx------  2 michalejackson michalejackson     4096 Feb  9 09:52 mc-michalejackson
drwxr-xr-x 12 michalejackson michalejackson     4096 Mar 11 11:35 ntools
drwxr-xr-x  2 michalejackson michalejackson     4096 Mar 11 11:31 p
drwxr-xr-x  8 michalejackson michalejackson     4096 Feb 24 12:46 pdk-michalejackson
-rw-------  1 michalejackson michalejackson    11379 Feb 15 17:20 OHdX99GtwS
-rw-------  1 michalejackson michalejackson    12288 Feb 15 14:13 grab_xml_structure.pl.swp
-rw-------  1 michalejackson michalejackson    23369 Mar  8 15:27 3vAI5C1pq0
-rw-------  1 root        root           98016 Mar 17 14:56 etherXXXXcv3NYF
-rw-------  1 root        root          111207 Mar 17 14:36 etherXXXXDC5v7g
-rw-r--r--  1 nagios      nagios        236075 Mar 22 10:02 fortune.log
-rw-------  1 root        root          480086 Mar 17 14:58 etherXXXXfdsy8y
-rw-------  1 root        root          499652 Mar 17 14:33 etherXXXXRi2cQQ
-rw-rw-r--  1 nagios      nagios      52680394 Mar 22 10:06 nagios-results.out
c

names removed to protect the guilty
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies

2. Shell Programming and Scripting

Sorting dates in Perl

I have a directory of backup files. named like this: ldap.data.04-06-2012.tar ldap.data.03-06-2012.tar ldap.data.02-06-2012.tar ldap.data.01-06-2012.tar ldap.data.31-05-2012.tar ldap.data.30-05-2012.tar ldap.data.29-05-2012.tar ldap.data.28-05-2012.tar ldap.data.27-05-2012.tar... (6 Replies)
Discussion started by: robsonde
6 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Sorting a particular column in PERL

I have a file abc.txt which contains data in th following format abc,23 hgfh,87 tweg,89 jdfjn,74 I want to sort on the basis of column (the second one). It should be numerical sort. output should be abc,23 jdfjn,74 hgfh,87 tweg,89 I know how to do it in unix. I need a PERL code (1 Reply)
Discussion started by: centurion_13
1 Replies

5. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

6. Shell Programming and Scripting

perl array sorting

Hi All, I have an array in perl as @match = (201001,201002,201001,201002); I am trying to sort this array as @match = sort(@match); print "@match"; I dont see the output sorted any answers I also tried another way, but still the results are not sorted foreach my $match (sort { $a... (2 Replies)
Discussion started by: bsdeepu
2 Replies

7. Shell Programming and Scripting

Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^ it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort. Please help me create the sub to pass to the sort function. (7 Replies)
Discussion started by: dangral
7 Replies

8. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

9. Shell Programming and Scripting

perl sorting

I have many files that I need to sort each week. I know how to do in Unix, but for this task it appears best to do native inside an existing perl program. So, simplified, I have a file similar to the following: Joe_________12_Main_St__A001________LX Benny_______5_Spring____A002________LX... (5 Replies)
Discussion started by: joeyg
5 Replies

10. Shell Programming and Scripting

Perl: sorting files by whats in'em

i'm halfway into my script and realize i may need to use an associative array (a hash, i guess they are called in Perl). i'm fairly new to Perl and don't know how to use them correctly. i have some files in a directory. each file contains a number in a specific place in it's header. what i would... (4 Replies)
Discussion started by: quantumechanix
4 Replies
Login or Register to Ask a Question