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
#!/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
[/COLOR]
You can use the following command for sorting the data by second column.
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
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
Thanks for reply,
I know how to do with sort -k2 file but i want to try with perl
ungalnanban:
You can use the following command for sorting the data by second column.
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
summink like:
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