#!perl =comment Being a compulsive list person, I was looking for a way of sorting my lists more automatically by tagging single lines or sections and letting a program sort through and reorganize the list depending on the tags. This program will take a single file argument and use the following tags to organize the content to a file 'list.out'. Untagged content is left at the top of the file as is. The 'XX' represents a category number either zero '01' or space padded ' 1'. Note to include a space character after the 'XX' as this is truncated from the output list also. #tags used >>>>cahXX #category header number XX >>>catXX #single line category entry <<<>>> #section close category entry Change the $cat_total and @list_order to change the total number of categories and the order they are output. Empty categories are not printed. #sample input file start >>>>cah01 home >>>cat01 home >>>cat01 ==================================== >>>>cah02 pets >>>cat02 pets >>>cat02 ==================================== <<<>>> >>>cat02 feed cats unmarked text <<<>>> more unmarked text >>>cat01 do laundry #sample input file end #output file start unmarked text more unmarked text >>>>cah 1 home >>>>cah 2 pets <<<>>> <<<>>> #output file end =cut use strict; my $currentFile = $ARGV[0]; my @str_arr = (); my @str_arr_header = (); my $str_buffer; my $cat; open (OUTPUT_FILE,">list.out"); my $line_record; open (INPUT_FILE, "./$currentFile"); foreach $line_record () { #single lines if ($line_record =~ '>>>cat') { $cat = substr($line_record,6,2); #print $cat; substr($line_record,0,9) = ''; @str_arr[$cat] .= $line_record; #print $line_record; $cat = ''; } #category headers elsif ($line_record =~ '>>>>cah') { $cat = substr($line_record,7,2); #print $cat; substr($line_record,0,10) = ''; @str_arr_header[$cat] = $line_record; #print $line_record; $cat = ''; } #open cat append elsif ($line_record =~ '<<<>>>') { #print $line_record; $cat = ''; } else { if ($cat) { @str_arr[$cat] .= $line_record; } else { #unmarked string buffer $str_buffer .= $line_record; } } } close(INPUT_FILE); #the total number of categories my $cat_total = 40; #currently 24 #the order in which the categories are listed my @list_order = (17,18,19,20,21,22,23,24,12,1,2,3,4,5,6,7,16,8,9,10,11,15,13,14); for (my $i=0; $i < $cat_total; $i++) { if (@str_arr[@list_order[$i]]) { print OUTPUT_FILE ">>>>cah".sprintf("%2d",@list_order[$i])." ".@str_arr_header[@list_order[$i]]; } } for (my $i=0; $i < $cat_total; $i++) { if (@str_arr[@list_order[$i]]) { print OUTPUT_FILE "<<<>>>\n"; } } print OUTPUT_FILE $str_buffer; close(OUTPUT_FILE); exit 0;