Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8155475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:40:46+00:00 2026-06-06T16:40:46+00:00

I have a file that looks like: SECTION1 id name sub section1 sub section2

  • 0

I have a file that looks like:

SECTION1 id name  
 sub section1
 sub section2
SECTION2 id name  
 sub section3
 sub section4
 sub section6
SECTION1 id name  
 sub section7
 sub section8
SECTION3 id name  
 sub section9
 sub section10
 sub section11
 sub section12
SECTION2 id name  
 sub section13
 sub section14
SECTION1 id name  
 sub section15
 sub section16
SECTION3 id name  
 sub section17
 sub section18

I need to sort this file section wise. The only thing I know is that I have ‘SECTION1’, ‘SECTION2’ AND ‘SECTION3’.
Expected output after sorting is:

SECTION1 id name  
 sub section1
 sub section2
SECTION1 id name  
 sub section7
 sub section8
SECTION1 id name  
 sub section15
 sub section16
SECTION2 id name  
 sub section3
 sub section4
 sub section6
SECTION2 id name  
 sub section13
 sub section14
SECTION3 id name  
 sub section9
 sub section10
 sub section11
 sub section12
SECTION3 id name  
 sub section17
 sub section18

Is there any easy way to do this in perl or using the utilities like grep,sed etc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:40:47+00:00Added an answer on June 6, 2026 at 4:40 pm

    One more way using perl:

    Assuming infile has the content of the question and following content of script.pl:

    use warnings;
    use strict;
    use sort qw/stable/;
    
    my ($section, @section);
    
    while ( <> ) { 
    
        ## Save text if first line or when line doesn't begin with 'SECTION' word.
        if ( $. == 1 || $_ !~ m/\ASECTION\d+/ ) { 
            $section .= $_; 
            next unless eof;
        }   
    
        ## Save the text and the number of section.
        if ( $section =~ m/\ASECTION(\d+)/ ) { 
            push @section, [ $1, $section ];
            $section = q||;
        }   
    
        ## Begin to save next section.
        $section .= $_; 
    }
    
    ## Print them sorted by section number.
    for ( sort { $a->[0] <=> $b->[0] } @section ) { 
        printf qq|%s|, $_->[1];
    }
    

    Run it like:

    perl script.pl infile
    

    With following output:

    SECTION1 id name  
     sub section1
     sub section2
    SECTION1 id name  
     sub section7
     sub section8
    SECTION1 id name  
     sub section15
     sub section16
    SECTION2 id name  
     sub section3
     sub section4
     sub section6
    SECTION2 id name  
     sub section13
     sub section14
    SECTION3 id name  
     sub section9
     sub section10
     sub section11
     sub section12
    SECTION3 id name  
     sub section17
     sub section18
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I have a file that looks like: 1 1 C C 1.9873 2.347 3.88776
I have a file that contain lines that looks like this: >AF001546_1 [88 -
I have a file that contain list of numbers that looks like this: 10^-92
I have a data file that looks like this: xyz123 2.000 -0.3974 0.0 hij123
I have a text file that looks like this. A 102 B 456 C
I have a file structure that looks like this ./501.res/1.bin ./503.res/1.bin ./503.res/2.bin ./504.res/1.bin and
I have a text file that looks like this: value1 value2 value3 There are
I have an app.config file that looks like this: <?xml version=1.0 encoding=utf-8 ?> <configuration>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.