ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Does anyone have a cunning way of sorting by field one and then subsorting by field 2 without losing the order of the first field? If I sort by field 2 then of course the 9 and 10 at the beginning would could out of order.
yes of course I manned sort before posting and I tried -g as well at that time but it doesn't give the desired effect, the sort still comes out wrong with .11 before .2 for example, same as demonstrated above.
Well, you can do it with a short Perl program. Be aware that this will slurp up the whole input file into memory, so if it's a huge file, this is probably not a good method.
Code:
#!/usr/bin/perl
# save this into a file called "mysort" and make that executable
# then invoke "mysort" on your input file. The sorted output will
# be printed on stdout, so you can re-direct it like this:
#
# mysort input_file > output_file
sub my_cmp {
@a=split(/\./, $a, 3);
@b=split(/\./, $b, 3);
if ($a[0] ne $b[0]) { return $a[0] <=> $b[0]; }
else { return $a[1] <=> $b[1]; }
}
foreach (sort my_cmp <>) { print; }
#!/bin/bash
list=($(cat infile | awk -F . '{print $1}' | sort -ug))
for i in $(seq 0 $((${#list[*]}-1)))
do
ftemp=$(mktemp)
egrep ^${list[${i}]}'\.' infile | awk -F . '{print $2}' >> ${ftemp}
list2=($(sort -n ${ftemp}))
for j in $(seq 0 $((${list2[*]}-1)))
do
echo ${list[${i}]}.${list2[${j}]}
done
rm ${ftemp}
done
Basically, what I've done is to sort the first field (list), then create a temporary file that contains only the second field for each list, and I sort that out into a second list (list2). Then it is a simple matter of echoing out the first field . second field. Then get rid of the temporary file at the end of each first field, though I suppose you could skip that if you want. This works if you have something like 1 and 10 in the same file as well (set my test data up to make sure).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.