Sponsored Content
Top Forums Shell Programming and Scripting Sorting lines between patterns in alphabetical order Post 302731013 by summer_cherry on Wednesday 14th of November 2012 04:56:59 AM
Old 11-14-2012
perl

Code:
local $/="<\/section>\n";
while(<DATA>){
  if(/(<section .*?>)(.*)<\/section>/ms){
	my $pre = $1;
	my $mid = $2;
	print $pre,"\n";
	my %hash =  $mid =~ /(<.*?>(.*?)<\/p>)/g;	
	print join "\n", sort {$hash{$a} cmp $hash{$b}} keys %hash;
	# print join "\n", values %hash;
	print "\n<\/section>\n";
  }
}
__DATA__
<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>

 

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
STRFILE(8)						    BSD System Manager's Manual 						STRFILE(8)

NAME
strfile, unstr -- create a random access file for storing strings SYNOPSIS
strfile [-iorsx] [-c char] source_file [output_file] unstr source_file DESCRIPTION
strfile reads a file containing groups of lines separated by a line containing a single percent '%' sign and creates a data file which con- tains a header structure and a table of file offsets for each group of lines. This allows random access of the strings. The output file, if not specified on the command line, is named source_file.dat. The options are as follows: -c char Change the delimiting character from the percent sign to char. -i Ignore case when ordering the strings. -o Order the strings in alphabetical order. The offset table will be sorted in the alphabetical order of the groups of lines refer- enced. Any initial non-alphanumeric characters are ignored. This option causes the STR_ORDERED bit in the header str_flags field to be set. -r Randomize access to the strings. Entries in the offset table will be randomly ordered. This option causes the STR_RANDOM bit in the header str_flags field to be set. -s Run silently; don't give a summary message when finished. -x Note that each alphabetic character in the groups of lines is rotated 13 positions in a simple caesar cipher. This option causes the STR_ROTATED bit in the header str_flags field to be set. The format of the header is: #define VERSION 1 unsigned long str_version; /* version number */ unsigned long str_numstr; /* # of strings in the file */ unsigned long str_longlen; /* length of longest string */ unsigned long str_shortlen; /* length of shortest string */ #define STR_RANDOM 0x1 /* randomized pointers */ #define STR_ORDERED 0x2 /* ordered pointers */ #define STR_ROTATED 0x4 /* rot-13'd text */ unsigned long str_flags; /* bit field for flags */ char str_delim; /* delimiting character */ All fields are written in big-endian byte order. The purpose of unstr is to undo the work of strfile. It prints out the strings contained in the file source_file in the order that they are listed in the header file source_file.dat to standard output. It is possible to create sorted versions of input files by using -o when strfile is run and then using unstr to dump them out in the table order. FILES
strfile.dat default output file. SEE ALSO
byteorder(3), fortune(6) HISTORY
The strfile utility first appeared in 4.4BSD. BSD
January 17, 2010 BSD
All times are GMT -4. The time now is 11:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy