Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Ordering string of commands in a file

Reply
Thread Tools

Ordering string of commands in a file

 
 
Raghuramaiah Gompa
Guest
Posts: n/a
 
      12-29-2005
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, .... )

How can I achieve this? Please help. .. Raghu
 
Reply With Quote
 
 
 
 
xicheng@gmail.com
Guest
Posts: n/a
 
      12-29-2005
you may need to reset $/ to read your data, and then use hash keys to
sort your results. see the following code.
-----------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
my %h=();
my $n;
{
local $/="Title";
while(<DATA>) {
chomp;
$h{$1}=$2 if(/\A\d+=(.*?)\n(.*)\z/sm);
}
}
foreach my $k(sort{lc $a cmp lc $b} keys %h) {
$n++;
print "Title$n=$k\n$h{$k}";
}
__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
-----------------------------------------------------------------------
Raghuramaiah Gompa 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, .... )
>
> How can I achieve this? Please help. .. Raghu


 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      12-29-2005
Raghuramaiah Gompa 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, .... )
>
> How can I achieve this? Please help. .. Raghu


Perhaps something like this:

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


my %hash = do { local $/; grep length, split /^(Title\d+=.+)/m, <DATA> };

print
map $_->[ 1 ],
sort { lc $a->[ 0 ] cmp lc $b->[ 0 ] }
map [ /=(.+)/, "$_$hash{$_}" ],
keys %hash;


__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




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Raghuramaiah Gompa
Guest
Posts: n/a
 
      12-31-2005
In article <43b506a6$0$898$>,
Dave Weaver <> wrote:
>Raghuramaiah Gompa <> 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
>


First of all, I apologize for not making my question
clear. However, you (and others, too) understood exactly
what I want. Thank you all for the codes and help. Now I
am faced with another problem. This sorting must be done
only for a part of the text that is between the
line that contain [Buttons] and someother line that
starts with [ (it could be [Directories] or [Files]).
Rest of the document should be copied as it is. I tried
several ways and failed Please help. .. Raghu
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      12-31-2005
Raghuramaiah Gompa wrote:
> This sorting must be done
> only for a part of the text that is between the
> line that contain [Buttons] and someother line that
> starts with [ (it could be [Directories] or [Files]).
> Rest of the document should be copied as it is. I tried
> several ways and failed


You generally get more helpful responses if you post what you
attempted, explain how it failed, and allow us to help you fix your
attempt, rather than simply claiming that you tried, and asking for the
solution outright.

> Please help.


Look up the "flip-flop" operator (which is the range operator, but used
in a scalar context) in
perldoc perlop

Paul Lalli

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-31-2005
Raghuramaiah Gompa <> wrote:

[ snip 120 lines of quoted text! ]

> I tried
> several ways and failed



Show us your code, and we will help you fix it.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Raghuramaiah Gompa
Guest
Posts: n/a
 
      01-01-2006
In article <. com>,
Paul Lalli <> wrote:
>Raghuramaiah Gompa wrote:
>> This sorting must be done
>> only for a part of the text that is between the
>> line that contain [Buttons] and someother line that
>> starts with [ (it could be [Directories] or [Files]).
>> Rest of the document should be copied as it is. I tried
>> several ways and failed

>
>You generally get more helpful responses if you post what you
>attempted, explain how it failed, and allow us to help
>you fix your
>attempt, rather than simply claiming that you tried,
>and asking for the
>solution outright.
>
>> Please help.

>
>Look up the "flip-flop" operator (which is the range
>operator, but used
>in a scalar context) in
>perldoc perlop
>
>Paul Lalli
>


Thank you for your response. Here is one that was very
close to getting what I want. But it is somehow printing
the whole file first and doing the reordering I wanted.


#!/usr/bin/perl

use warnings;
use strict;
my %par;


my %items;
print "[Options]\n";
while(<>) {
$items{$2}{$1} = $3 if /^(\w+?)(\d+)=(.+)/;
print if /[Options]\n/ .. /[Buttons]\n/;
#the above line is the problem. I just want the lines
#between [Options] and [Buttons]
#be printed including these lines.
}
print "[Buttons]\n";
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;
}

Please help. .. Raghu
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-01-2006
Raghuramaiah Gompa wrote:
> my %items;
> print "[Options]\n";
> while(<>) {
> $items{$2}{$1} = $3 if /^(\w+?)(\d+)=(.+)/;
> print if /[Options]\n/ .. /[Buttons]\n/;
> #the above line is the problem. I just want the lines
> #between [Options] and [Buttons]
> #be printed including these lines.
> }


[ and ] are special characters in a regular expression. You need to
escape them.

print if /\[Options\]\n/ .. /\[Buttons\]\n/;

(as it stands, the first reg ex is true if it contains any of O, p, t,
i, o, n, or s.)

Paul Lalli

 
Reply With Quote
 
Raghuramaiah Gompa
Guest
Posts: n/a
 
      01-01-2006
In article <43b506a6$0$898$>,
Dave Weaver <> wrote:
>
>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



Thank you all very much. I was able to get the results I
wanted. (I posted code and Xicheng pointed out my
mistake in ignoring the necessary \ before [ and ]). As
you noticed, group of commands with no title was
processed with no title. I do not know how to fix (this
minor) problem. I am very happy with the present code.
But could it be improved? Thank you once again. .. Raghu

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-01-2006
Raghuramaiah Gompa wrote:
> my %items;
> print "[Options]\n";
> while(<>) {
> $items{$2}{$1} = $3 if /^(\w+?)(\d+)=(.+)/;
> print if /[Options]\n/ .. /[Buttons]\n/;
> #the above line is the problem. I just want the lines
> #between [Options] and [Buttons]
> #be printed including these lines.
> }


[ and ] are special characters in a regular expression. You need to
escape them.

print if /\[Options\]\n/ .. /\[Buttons\]\n/;

(as it stands, the first reg ex is true if it contains any of O, p, t,
i, o, n, or s.)

Paul Lalli

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Z-Ordering (Morton ordering) question nbigaouette C Programming 2 11-06-2009 05:26 AM
ConfigParser preserving file ordering Frank Aune Python 0 10-18-2007 11:23 PM
using std::string; string("hello") vs std::string("hello") in header file. Fei Liu C++ 9 04-01-2006 08:49 AM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57