Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Issue 3

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
0
0
US
Hi,

I am trying this for some time now, and could not get to resolve this. Some one of you may be able to help me.

I have a text file with about 2000 lines. The file is of format as below


Book1
.....
.....
.....
Price1:10:00
.....
.....
.....
.....
Book2
.....
.....
Price1:10:50
.....
......
......
Book1
.....
......
Price1:9:99
.....
.....
Book2
.....
......
Price1:8:99

I need to format this text file to get an output like below


Book1 10:00 9.99
Book2 10:50 8.99

and so on......

Any help is really appreciated.

Thanks,
dbadmin

 
We'll need a bit more to go on than that. Reformatting the data in the way you want is easy, but identifying which bits to extract is more tricky. I'm assuming that Book1 - Bookn are the keys, and you just want to list all the prices that occur for those books. But your description of the file layout is too generic. Can you post a sample of the real input file, say about 30 lines?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi Steve,

Thanks for the quick reply. This is the sample

Cook Book
Mary Ronald
Mc Graw Hill Pub
ABC Book Store
Price1:10:00

Library Science Handbook
Robert Baldwin
Penguin Pub
Meerat Books
Price1:9.99

Cancer Treatment
Dr. Phil Marinow
Mc Graw Hill Pub
Tenin Bookshop
Price1:11:50

Cook Book
Mary Ronald
Mc Graw Hill Pub
Tenin Bookshop
Price1:11:00

Library Science Handbook
Robert Baldwin
Penguin Pub
ABC Bookstore
Price1:12:00

Cancer Treatment
Dr. Phil Marinow
Mc Graw Hill Pub
Meerat Books
Price1:8:50


I need the output like

Cook Book 10:00 11:00
Library Science Handbook 9:99 12:00
Cancer Treatment 11:50 8:50


Thanks,
dbadmin




 
Parsing's a bit crude, i.e. it will break if you have less than six lines for each entry.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my (%books, $key);

while (<DATA>) {
   chomp;
   $key = $_ if ($. % 6 == 1);
   push @{$books{$key}}, $1 if (/Price1:(\d+[:.]\d+)/);
}

foreach (sort keys %books) {
   print $_, "\t", join("\t", @{$books{$_}}), "\n";
}

__DATA__
Cook Book
Mary Ronald
Mc Graw Hill Pub
ABC Book Store
Price1:10:00

Library Science Handbook
Robert Baldwin
Penguin Pub
Meerat Books
Price1:9.99

Cancer Treatment
Dr. Phil Marinow
Mc Graw Hill Pub
Tenin Bookshop
Price1:11:50

Cook Book
Mary Ronald
Mc Graw Hill Pub
Tenin Bookshop
Price1:11:00

Library Science Handbook
Robert Baldwin
Penguin Pub
ABC Bookstore
Price1:12:00

Cancer Treatment
Dr. Phil Marinow
Mc Graw Hill Pub
Meerat Books
Price1:8:50
I've used tabs to separate them, you might not want this.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
essentially the same as Steves code above but less data file format sensitive:

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]%books[/blue], [blue]$key[/blue][red])[/red][red];[/red]

[maroon]MAINLOOP[/maroon][maroon]:[/maroon] [olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\w[/b][/purple][/purple][red]/[/red][red])[/red] [red]{[/red]
      [black][b]my[/b][/black] [blue]$key[/blue] = [blue]$_[/blue][red];[/red] 
      [olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
         [black][b]chomp[/b][/black][red];[/red]
         [olive][b]next[/b][/olive] MAINLOOP [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red])[/red][red];[/red] 
         [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$books[/blue][red]{[/red][blue]$key[/blue][red]}[/red][red]}[/red], [blue]$1[/blue] [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]Price1:([purple][b]\d[/b][/purple]+[:.][purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red])[/red][red];[/red]
      [red]}[/red]
   [red]}[/red]      
[red]}[/red]

[olive][b]foreach[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%books[/blue][red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$_[/blue], [red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url][red]([/red][red]"[/red][purple][purple][b]\t[/b][/purple][/purple][red]"[/red], [blue]@[/blue][red]{[/red][blue]$books[/blue][red]{[/red][blue]$_[/blue][red]}[/red][red]}[/red][red])[/red], [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]Cook Book[/teal]
[teal]Mary Ronald[/teal]
[teal]Mc Graw Hill Pub[/teal]
[teal]ABC Book Store[/teal]
[teal]Price1:10:00[/teal]

[teal]Library Science Handbook[/teal]
[teal]Robert Baldwin[/teal]
[teal]Penguin Pub[/teal]
[teal]Meerat Books[/teal]
[teal]Price1:9.99[/teal]

[teal]Cancer Treatment[/teal]
[teal]Dr. Phil Marinow[/teal]
[teal]Mc Graw Hill Pub[/teal]
[teal]Tenin Bookshop[/teal]
[teal]Price1:11:50[/teal]

[teal]Cook Book[/teal]
[teal]Mary Ronald[/teal]
[teal]Mc Graw Hill Pub[/teal]
[teal]Tenin Bookshop[/teal]
[teal]Price1:11:00[/teal]

[teal]Library Science Handbook[/teal]
[teal]Robert Baldwin[/teal]
[teal]Penguin Pub[/teal]
[teal]ABC Bookstore[/teal]
[teal]Price1:12:00[/teal]

[teal]Cancer Treatment[/teal]
[teal]Dr. Phil Marinow[/teal]
[teal]Mc Graw Hill Pub[/teal]
[teal]Meerat Books[/teal]
[teal]Price1:8:50[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
In the name of TMTOWTDI, here's another way (though I actually like Kevin's solution better). If for some reason you want the other text, like the author or the publisher, it's in @text.

Code:
open FILE, "sample.txt";
my %list;
while (<FILE>) {
  my @text;
  do {
    if ($_ =~ /[a-z]+/i) {
      chomp $_;
      push @text, $_;
    }
  } while (($_ = <FILE>) !~ /Price1:/);
  chomp $_;
  push @{$list{$text[0]}}, substr $_, 7;
}
for my $name (keys(%list)) {
  print $name;
  for (@{$list{$name}}) {
    print "  ".$_;
  }
  print "\n";
}
close FILE;

 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Tek[/green]-Tips [red]qw([/red][purple]Perl_Forum[/purple][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$stanious[/blue] = Tek-Tips->[maroon]new[/maroon][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] Welcome->[blue]$stanious[/blue][red];[/red]
[tt]------------------------------------------------------------
Other Modules used :
[ul]
[li]Tek[/li]
[/ul]
[/tt]

[smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Gurus,

You all really rock! I used Kevin's solution. Thank You so much!!!

dbadmin
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top