Re: Ordering string of commands in a file



Raghuramaiah Gompa <rgompa@xxxxxxxxxxxxxxxxxxxxx> wrote:
> I have a file that has lines like the following:
>
> Titl1=B2a
> Command1=c:\chi\cw.exe
> Title2=f2a
> Directory1=c:\chi
> Type1=1
> Command2=c:\chi\cw.exe
> Directory2=c:\chi
> Options2=%@%
> Type11=2
> Title14=Ma3
> Command14=c:\chi\cpb.bat
> Options14=c:\temp\temp.bin
> Use long names3=1
> Title12=Dir
> Command12=Tfd
> Title13=a2h
> Command13=c:\bat\a2h.bat
> Options13=%@dpn%
> Show13=2
> Command31=C:\bat\tview.bat
> Use long names31=1
> Title31=tview
> .....
>
> Basically Title,Command, Show,... constitute a group of commands
> that are associated with the name described in Title. Now I want
> to reorder them in the alphabetical order of names described in
> Title. For instance, the new file should have :
>
> Title1=a2h Command1=c:\bat\a2h.bat Options1=%@dpn%
> Show1=2
>
> and all the lines (Title2=B2a, ..) are changed accordingly (because
> or the order a2h, B2a, Dir, f2a, Ma3, .... )

Your description is not very clear.

You want to group the lines where the number is in common (so Title14
goes with Options14 and Command14, and so forth)?

Then sort the groups them based on the value of the Title element?
What about when there is no Title elelement, such as in Type11
above?

Then ouput in this new order, but renumber them, so the the first
group is always Title1=... , then second has Title2=... and so on?

Is that what you mean?

The code below *might* do something like you want, but since your
specification is lacking, the results may be too.

Interesting (to me) sidenote: I noticed that when I added the lc()
calls to the sort, the warnings about use of undefined variables
(because Title isn't defined for some elements) went away. Shouldn't
lc() warn if it's passed undef?


#!/usr/bin/perl
use warnings;
use strict;

my %items;
while(<DATA>) {
$items{$2}{$1} = $3 if /^(\w+?)(\d+)=(.+)/;
}

my $n = 1;
for my $idx (
sort {
lc $items{$a}{Title} cmp lc $items{$b}{Title}
} keys %items)
{
print map {
"$_$n=$items{$idx}{$_}\n";
} keys %{$items{$idx}};
print "----\n";
++$n;
}

__DATA__
Title1=B2a
Command1=c:\chi\cw.exe
Title2=f2a
Directory1=c:\chi
Type1=1
Command2=c:\chi\cw.exe
Directory2=c:\chi
Options2=%@%
Type11=2
Title14=Ma3
Command14=c:\chi\cpb.bat
Options14=c:\temp\temp.bin
Use long names3=1
Title12=Dir
Command12=Tfd
Title13=a2h
Command13=c:\bat\a2h.bat
options13=%@dpn%
Show13=2
Command31=C:\bat\tview.bat
Use long names31=1
Title31=tview

.



Relevant Pages

  • Re: Ordering string of commands in a file
    ... >of commands ... >> to reorder them in the alphabetical order of names ... >calls to the sort, the warnings about use of undefined ...
    (comp.lang.perl.misc)
  • Re: parsing Makefiles
    ... > use warnings; ... This code works just fine providing that the Makefile commands don't output anything. ... Andrew Gaffney ...
    (perl.beginners)
  • Re: Editing a huge text file - whats best?
    ... smaller files, again with a cli program of some sort, since the sql ... commands will still work as long as the file is split at line breaks. ...
    (uk.comp.sys.mac)
  • RE: Hash Issue
    ... Subject: Hash Issue ... > That works ok but still doesn't sort my keys correctly ... > Even if I use sort like so doesn't work. ... The above sorts the OS's not the commands... ...
    (perl.beginners)
  • Re: Earthquake and tornado data evaluation Perl program Apr. 14, 2004
    ... Rather than try to respond to individual notes that have been posted ... regarding using certain Perl commands in that program I am going to try to ... Perl's SORT command offers quite a few options. ... But on my system those sorts process more than 20,000 lines of ...
    (comp.lang.perl.misc)