Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > extract block of text

Reply
Thread Tools

extract block of text

 
 
mike
Guest
Posts: n/a
 
      11-17-2003
hi
i have some data contained in a file like this:

/* ----------------- Heading1 ----------------- */

line: Heading1 type: b
#owner: http://www.velocityreviews.com/forums/(E-Mail Removed)


/* ----------------- Heading2 ----------------- */

line: Heading2 type: c
command: echo "hi"
owner: (E-Mail Removed)
machine: server


/* ----------------- Heading3 ----------------- */

.....


how can i extract the data from "Heading2" to "Heading3" such that
i have this output without the "Headings" and without the empty lines.

line: Heading2 type: c
command: echo "hi"
owner: (E-Mail Removed)
machine: server

thanks for any help
 
Reply With Quote
 
 
 
 
peter pilsl
Guest
Posts: n/a
 
      11-17-2003
mike wrote:

>
> how can i extract the data from "Heading2" to "Heading3" such that
> i have this output without the "Headings" and without the empty lines.
>
> line: Heading2 type: c
> command: echo "hi"
> owner: (E-Mail Removed)
> machine: server
>
> thanks for any help
>


even if OT, cause no perl-question included:

standard approach:
you read the input line per line. If you reach Heading2, set a flag. If you
reach Heading3 clear the flag. For each line that is not empty check if the
flag is set - if yes print out the line. (its more efficient to check the
flag first and then if the line is empty)

if the inputfile is known to be of limited length, you can read all the
file at once by altering the $/-variable and use the m//-operator to get
the Text between Heading2 and Heading3 and the s///-operator to eliminate
empty lines.

If you have any questions regarding one of these steps please feel free to
ask again and dont forget to post part of your source so we can help you
even better.

best,
peter

--
peter pilsl
(E-Mail Removed)
http://www.goldfisch.at

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-17-2003
Christoph Schuch <(E-Mail Removed)> wrote:

> **** Post for FREE via your newsreader at post.usenet.com ****

^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^


I'll add that to my scorefile. Thanks.


> The following Code should handle your task
>
>
> open(filein,"<file")



That has a syntax error.

You should use UPPER CASE filehandles.

You should always, yes *always*, check the return value from open():

open(FILEIN, 'file') or die "could not open 'file' $!";


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-17-2003
mike <(E-Mail Removed)> wrote:

> how can i extract the data from "Heading2" to "Heading3" such that
> i have this output without the "Headings" and without the empty lines.



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

my $record = '';
while ( <DATA> ) {
if ( m#/\* ----------------- Heading\d+ ----------------- \*/# ) {
$record =~ s/^\s+//;
$record =~ s/\s+$//;
print "$record\n-----\n";
$record = ''; # clear buffer
}
else {
$record .= $_;
}
}

# final record
$record =~ s/^\s+//;
$record =~ s/\s+$//;
print "$record\n-----\n";

__DATA__
/* ----------------- Heading1 ----------------- */

line: Heading1 type: b
#owner: (E-Mail Removed)


/* ----------------- Heading2 ----------------- */

line: Heading2 type: c
command: echo "hi"
owner: (E-Mail Removed)
machine: server


/* ----------------- Heading3 ----------------- */

.....
--------------------------------------------------------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-17-2003

(E-Mail Removed) (mike) wrote:
> i have some data contained in a file like this:
>

<snip>
> /* ----------------- Heading2 ----------------- */
>
> line: Heading2 type: c
> command: echo "hi"
> owner: (E-Mail Removed)
> machine: server
>
>
> /* ----------------- Heading3 ----------------- */

<snip>

> how can i extract the data from "Heading2" to "Heading3"

[without the blank lines]

<untested>

perl -ne'next if /^\s*$/; print if m|/* -+ Heading2| .. m|/* -+ Heading3|'

Ben

--
It will be seen... that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher arises among them who... convinc[es] them that their
....institutions are not based on... morality. [Samuel Butler] (E-Mail Removed)
 
Reply With Quote
 
Sara
Guest
Posts: n/a
 
      11-17-2003
"Christoph Schuch" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> **** Post for FREE via your newsreader at post.usenet.com ****
>
> The following Code should handle your task
>
>
> open(filein,"<file")
>
> while ($line=<filein>) {
>
> if ( $line =~ m/\/\* -+ Heading2 -+ \*\/ ) { $flag=0 };
>
> if ( $flag == "1" && ! ( $line =~ m/^$/ )) {
> print $line;
> }
>
> if ( $line =~ m/\/\* -+ Heading1 -+ \*\/ ) { $flag=1 };
> }
>
>
> christoph
>
>
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
> http://www.usenet.com
> Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=




Bleech- if you're looping through this data line by line you might as
well use Basic or Fortran.. and if you INSIST on looping at least
slurp the file into an array with my @f = <F> , close the file, then
loop over the array (its debuggable for one thing).

But hey let's use the power of Perl as long we we're using Perl? I'm
not going to solve your entire problem but here is a nice approach
(with no looping) and it has a much more useful output should your
requirements change later (as they often do in the real world) (don;t
ya hate parenthetical expressions?):

#!/usr/bin/perl -wd

$_ =
'/* ----------------- Heading1 ----------------- */

line: Heading1 type: b
#owner: (E-Mail Removed)


/* ----------------- Heading2 ----------------- */

line: Heading2 type: c
command: echo "hi"
owner: (E-Mail Removed)
machine: server


/* ----------------- Heading3 ----------------- */


line: Heading3 type: x
command: echo "hi"
owner: (E-Mail Removed)
machine: server

';

@a = split /\n*\/\*\s*\-+\s*Heading(\d+)[^\n]+\n\n*/;
shift @a unless $a[0]; # toss off empty element
my %a = @a;

print "done\n";

************************************************** ***********************

The neat part is you have what you wanted (cleaned up blocks), but
stored in a HASH with a hashkey that is the header number, voila!


DB<1> x %a
0 1
1 'line: Heading1 type: b
#owner: (E-Mail Removed)'
2 3
3 'line: Heading3 type: x
command: echo "hi"
owner: (E-Mail Removed)
machine: server

'
4 2
5 'line: Heading2 type: c
command: echo "hi"
owner: (E-Mail Removed)
machine: server'


So now you can do whatever you like with the blocks, and in fact the
whole program was really like 3 lines, no loops, very easily debugged
and changed.

G
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-18-2003
On Mon, 17 Nov 2003 11:08:10 -0800, Sara wrote:
>> while ($line=<filein>) {
>> [...]


> Bleech- if you're looping through this data line by line you might as
> well use Basic or Fortran..


There are times when it's a wise thing to loop through data.


--
Tore Aursand <(E-Mail Removed)>
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      11-18-2003
On Mon, 17 Nov 2003 16:51:28 +0000 (UTC),
Ben Morrow <(E-Mail Removed)> wrote:
>
> <untested>
>
> perl -ne'next if /^\s*$/; print if m|/* -+ Heading2| .. m|/* -+ Heading3|'
>


ITYM:

perl -ne'next if /^\s*$/; print if m|/\* -+ Heading2| .. m|/\* -+ Heading3|'

<also untested>

Dave.

 
Reply With Quote
 
Sara
Guest
Posts: n/a
 
      11-18-2003
Tore Aursand <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> On Mon, 17 Nov 2003 11:08:10 -0800, Sara wrote:
> >> while ($line=<filein>) {
> >> [...]

>
> > Bleech- if you're looping through this data line by line you might as
> > well use Basic or Fortran..

>
> There are times when it's a wise thing to loop through data.


Yes, and this doesn't appear to be one of those times..
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-18-2003
Sara <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Tore Aursand <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)>.. .
> > On Mon, 17 Nov 2003 11:08:10 -0800, Sara wrote:
> > >> while ($line=<filein>) {
> > >> [...]

> >
> > > Bleech- if you're looping through this data line by line you might as
> > > well use Basic or Fortran..

> >
> > There are times when it's a wise thing to loop through data.

>
> Yes, and this doesn't appear to be one of those times..


How would you know?

The main reason *for* file slurping is non-sequential access to parts of
the file. Picking out some matches can be done sequentially, you don't
*need* the file content in memory for that.

The main reason *against* file slurping is large file size (current or
expected). We know nothing about that.

The only reason for file slurping in this situation would be laziness.
That doesn't rule it out as a good solution, but it doesn't make it
the method of choice.

Anno
 
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
Please help me how is easiest way to extract text between some variable text Mladen Perl Misc 5 02-22-2011 10:57 AM
Extract local variables from a block? Josh French Ruby 0 07-04-2008 12:30 AM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM



Advertisments