Sponsored Content
Top Forums Shell Programming and Scripting Sorting lines between patterns in alphabetical order Post 302730799 by birei on Tuesday 13th of November 2012 04:52:43 PM
Old 11-13-2012
Hi pioavi,

One way using perl and the module XML::Twig.
Code:
$ cat infile                                                                                                                                                                                                                                 
<root>                                                                                                                                                                                                                                       
  <section status="ole-service">                                                                                                                                                                                                             
  <p service="OOO">XZZ</p>                                                                                                                                                                                                                   
  <p service="AAA">AAA BBC</p>                                                                                                                                                                                                               
  <p service="XYZ">XYZ</p></section>                                                                                                                                                                                                         

  <section status="ole-service">
  <p service="ABCD">ABC</p>
  <p service="BDE">B-DE (FGH)</p>
  <p service="XYZ">XY-YZ</p>
  <p service="MNO">M-NO</p>
  <p service="NOPQ">NOPQ</p>
  <p service="BCD">BCDE</p>
  <p service="ECM">eCM</p></section>
</root>
$ cat script.pl
#!/usr/bin/perl

use strict;
use warnings;
use XML::Twig;

{
        my $twig = XML::Twig->new(
                twig_handlers => {
                        'section[@status="ole-service"]' => sub {
                                my @childs = 
                                        map { $_->[0] } 
                                        sort { $a->[1] cmp $b->[1] } 
                                        map { [$_, do { my $t = $_->text; $t =~ s/\W//g; lc $t } ] } 
                                        $_->cut_children( 'p' );

                                for my $c ( @childs ) {
                                        $c->paste( last_child => $_ );
                                }
                        },
                },
                pretty_print => 'indented',
        );

        $twig->parsefile( shift );
        $twig->print;
}
$ perl-5.14.2 script.pl infile
<root>
  <section status="ole-service">
    <p service="AAA">AAA BBC</p>
    <p service="XYZ">XYZ</p>
    <p service="OOO">XZZ</p>
  </section>
  <section status="ole-service">
    <p service="ABCD">ABC</p>
    <p service="BCD">BCDE</p>
    <p service="BDE">B-DE (FGH)</p>
    <p service="ECM">eCM</p>
    <p service="MNO">M-NO</p>
    <p service="NOPQ">NOPQ</p>
    <p service="XYZ">XY-YZ</p>
  </section>
</root>

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Order sorting

How do you sort text in order using sed? :confused: For example 01 B D A C to 01 ABCD (3 Replies)
Discussion started by: evoGage
3 Replies

2. Shell Programming and Scripting

shell program for sorting strings in an alphabetical order

Hi, I trying to find the solution for writing the programming in unix by shell programming for sorting thr string in alphabetical order. I getting diffculty in that ,, so i want to find out the solution for that Please do needful Thanks Bhagyesh (1 Reply)
Discussion started by: bp_vanarse
1 Replies

3. Shell Programming and Scripting

alphabetical order with out using sort command

hai, how can i sort a file alphabetically without using sort command (6 Replies)
Discussion started by: rahul801
6 Replies

4. UNIX for Dummies Questions & Answers

How can I list the file under a directory both in alphabetical and in reverse alphabetical order?

How can I list the file under current directory both in alphabetical and in reverse alphabetical order? (1 Reply)
Discussion started by: g.ashok
1 Replies

5. Shell Programming and Scripting

Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

6. Shell Programming and Scripting

[SHELL] Userlist alphabetical order

Hi everyone! I am new to the forum and have recently started working with Linux. Quick question, I want a user list in alphabetical order as the output of a shell script. Who can help me!? Thanks! From the netherlands ;) (5 Replies)
Discussion started by: dennisbest85
5 Replies

7. Shell Programming and Scripting

multiple sorting with different order

Hi Guys, I have data like this HOS05 23/12/2008 10AM HOS06 15/12/2008 2PM HOS62 29/12/2008 10AM HOS64 23/12/2008 2PM HOS70 26/12/2008 10AM ZFT01 06/12/2008 10AM HOS73 11/12/2008 2PM MHOS0 05/12/2008 10AM MHOS0 20/12/2008 2PM MHOS0 27/12/2010 2PM MHOS0 11/12/2008 10AM MHOS0 30/12/2009... (1 Reply)
Discussion started by: ckarunprakash
1 Replies

8. UNIX for Dummies Questions & Answers

Script to list applications in alphabetical order

I've looking over a script for work and I've had a problem with the script not listing the files in alphabetical order. To look up PIDs for apps, it would be beneficial to have them listed in that order. Here is what I've been reviewing. #!/usr/bin/perl $str = sprintf "%4s %-40s", "PID",... (7 Replies)
Discussion started by: whysolucky
7 Replies

9. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

10. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies
Bio::Phenotype::MeSH::Term(3pm) 			User Contributed Perl Documentation			   Bio::Phenotype::MeSH::Term(3pm)

NAME
Bio::Phenotype::MeSH::Term - A MeSH term SYNOPSIS
use Bio::Phenotype::MeSH::Term; # create a term object my $term = Bio::Phenotype::MeSH::Term->new (-id => 'D000001', -name => 'Dietary Fats', -description => 'dietary fats are...' ); # get a Bio::Phenotype::MeSH::Twig somehow... $term->add_twig($twig1); DESCRIPTION
This class keeps information about MeSH terms. MeSH stands for Medical Subject Headings and is one of the ways for annotaing biomedical literature. The terminology is maintained by National Library of Medicine of USA . See http://www.nlm.nih.gov/mesh/meshhome.html. In addition to id, name and description a term can know about its surrounding terms (Bio::Phenotype::MeSH::Twig) in the term hierarchy. This class is mainly used from Bio::DB::MeSH which retrieves terms over the Web. SEE ALSO
Bio::DB::MeSH, Bio::Phenotype::MeSH::Twig FEEDBACK
Mailing Lists User feedback is an integral part of the evolution of this and other Bioperl modules. Send your comments and suggestions preferably to the Bioperl mailing lists Your participation is much appreciated. bioperl-l@bioperl.org - General discussion http://bioperl.org/wiki/Mailing_lists - About the mailing lists Support Please direct usage questions or support issues to the mailing list: bioperl-l@bioperl.org rather than to the module maintainer directly. Many experienced and reponsive experts will be able look at the problem and quickly address it. Please include a thorough description of the problem with code and data examples if at all possible. Reporting Bugs report bugs to the Bioperl bug tracking system to help us keep track the bugs and their resolution. Bug reports can be submitted via the web: https://redmine.open-bio.org/projects/bioperl/ AUTHOR
Heikki Lehvaslaiho, heikki-at-bioperl-dot-org APPENDIX
The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ id Title : id Usage : $obj->id( "r1" ); or print $obj->id(); Function: Set/get for the id. Returns : A id [scalar]. Args : A id [scalar] (optional). name Title : name Usage : $obj->name( "r1" ); or print $obj->name(); Function: Set/get for the name. Returns : A name [scalar]. Args : A name [scalar] (optional). description Title : description Usage : $obj->description( "r1" ); or print $obj->description(); Function: Set/get for the description. Returns : A description [scalar]. Args : A description [scalar] (optional). add_synonym Title : add_synonym Usage : $obj->add_synonym( @synonyms ); or $obj->add_synonym( $synonym ); Function: Pushes one or more synonyms for the term term into the list of synonyms. Returns : Args : scalar(s). each_synonym Title : each_synonym() Usage : @gs = $obj->each_synonym(); Function: Returns a list of gene symbols [scalars, most likely Strings] associated with this phenotype. Returns : A list of scalars. Args : purge_synonyms Usage : $obj->purge_synonym(); Function: Deletes the list of synonyms to this term. Returns : A list of scalars. Args : Twig management Each MeSH term belongs to a complex tree like hierarchy of terms where each term can appear multiple times. The immediately surrounding nodes of the tree are modelled in twigs. See: Bio::Phenotype::MeSH::Twig. add_twig Title : add_twig Usage : $obj->add_twig( @twigs ); or $obj->add_twig( $twig ); Function: Pushes one or more twig term names [scalars, most likely Strings] into the list of twigs. Returns : Args : scalar(s). each_twig Title : each_twig() Usage : @gs = $obj->each_twig(); Function: Returns a list of gene symbols [scalars, most likely Strings] associated with this phenotype. Returns : A list of scalars. Args : purge_twigs Usage : $obj->purge_twig(); Function: Deletes the list of twigs associated with this term. Returns : A list of scalars. Args : each_parent Title : each_parent() Usage : @gs = $obj->each_parent(); Function: Returns a list of names of parents for this term Returns : A list of scalars. Args : perl v5.14.2 2012-03-02 Bio::Phenotype::MeSH::Term(3pm)
All times are GMT -4. The time now is 04:13 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy