LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 02-05-2009, 03:36 PM   #1
czezz
Member
 
Registered: Nov 2004
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 523

Rep: Reputation: 30
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,390
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
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
Location: Poland/Warsaw
Distribution: Slackware/Solaris
Posts: 523

Original Poster
Rep: Reputation: 30
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: 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:
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


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 04:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration