LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2009, 03:36 PM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
bash regex - sort


Here is a sample of a file.
File is divided into a section called: case "some_number"
As you can see numbers are set randomly.
What I need to achive is to sort all case sections beginning from the lowest to highest, so case "4" is 1st; case "111" is 2nd and case "309" is 3rd.

The problem is that content of each case sections must be kept.
Is this possible ?


Code:
case "309": ### - asdasdscxczx
       @string_1 = "irgoirhgoi;rhgv;rhg;rdsh;sdhfkjdsbhjbdjsv"
       @string_2 = @Mafda + @Aasdant + @asdsas
       string_3($*) # #$lslslslslsr,$psdfdsfgsdfdsfdste
       $string_4

case "4": ### - fgvbfdvcxvcx
       @string_1 = "asdsadefesdvfgwegfvrds"
       @string_2 = @ssfMsdfdsa + @asdsadsant + @dfdsgrgrv
       string_3($*) # #$asdfdsafsadsaadwfcedsfefcerc
       $string_4

case "111": ### - fgfb vrdsadsadxca
       @string_1 = "irgoirhgoi;rhgv;rhg;rdsh;sdhfkjdsbhjbdjsv"
       @string_2 = @Mafda + @Aasdant + @asdsas
       string_3($*) # #sadsdsadslslsr,$pasdasdsasdsxa
       $string_4
 
Old 02-05-2009, 04:19 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Not a bash solution, but that is the wrong hammer for this kind of nail.
Code:
#! /usr/bin/perl -w
# -------- LQczezz.pl -----------
use strict;
$/ = "\n\n";
    my @wholeFile = <>;
    foreach my $case ( sort byCaseNumber @wholeFile ){    
        print $case;
    }

sub byCaseNumber{
    $a =~ m/case\s+"(\d+)"/;
    my $caseNumA = $1;
    $b =~ m/case\s+"(\d+)"/;
    my $caseNumB = $1;
    if( $caseNumA > $caseNumB ){ return 1; }
    if( $caseNumA < $caseNumB ){ return -1; }
    return 0;
}
Run with sample file as commandline argument.
--- rod.
 
Old 02-05-2009, 04:42 PM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Well... I must say I like that hammer
Many thanks.
 
Old 02-05-2009, 07:41 PM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
The OP has a good answer, but I was thinking about this post on the subway ride home before I saw the answer. So in the spirit of TIMTOWTDI, here's my variant on it:
Code:
#!/usr/bin/env perl
use warnings;
use strict;

my @blocks = do { local $/ = "\n\n"; <> };

@blocks = sort { get_num($a) <=> get_num($b) } @blocks;

print @blocks;

sub get_num{
  my $item = shift;
  $item =~ m/case "(\d+)"/;
  return $1;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash : crop some text with regex dodoecchi Programming 2 01-30-2009 01:10 PM
Need help with bash + regex onesikgypo Programming 2 01-10-2009 10:08 AM
Regex in bash? BeholdMyGlory Linux - Newbie 4 01-10-2009 08:32 AM
bash: better way to delete files not matching a regex? pbhj Programming 8 10-15-2007 03:05 PM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 05:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration