Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-05-2009, 04:36 PM   #1
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 712

Rep: Reputation: 39
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 ?

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

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

case "111": ### - fgfb vrdsadsadxca
       @string_1 = "irgoirhgoi;rhgv;rhg;rdsh;sdhfkjdsbhjbdjsv"
       @string_2 = @Mafda + @Aasdant + @asdsas
       string_3($*) # #sadsdsadslslsr,$pasdasdsasdsxa
Old 02-05-2009, 05:19 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
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.
#! /usr/bin/perl -w
# -------- -----------
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, 05:42 PM   #3
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 712

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

Rep: Reputation: 59
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:
#!/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;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:10 PM
Need help with bash + regex onesikgypo Programming 2 01-10-2009 11:08 AM
Regex in bash? BeholdMyGlory Linux - Newbie 4 01-10-2009 09:32 AM
bash: better way to delete files not matching a regex? pbhj Programming 8 10-15-2007 04:05 PM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 06:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 AM.

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