Printing lines out of a text file

E

EmJayEm

I have a text file with contents:

ENTRY= entry 1
here goes the contents for entry 1
here goes the contents for entry 1
here goes the contents for entry 1
ENTRY= entry 2
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2
ENTRY= entry 3
here goes the contents for entry 3
here goes the contents for entry 3
here goes the contents for entry 2

In the text file, entry 1 is on line 1, entry 2 is on line 5, entry 3 is on
line 9.

I wish to display just one entry. eg. entry 2, like so:
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2

I've done it like this:

# get the entry's line number in the text file

while (! file[$line_of_file]=~m/ENTRY=(.*)/) # for each line of the entry
until the next entry.
{
print file[$line_of_file]; # print the line of the entry
}

For printing entry 3 the condition in the while loop has to test the end of
the file, like so:

while ("$line_of_file" eq "$total_number_of_lines_in_file") # while not
end of file
{
print file[$line_of_file]; # print the line of the entry
}

I've tried to combine the two condition statements into one while loop as
below but it didn't work, yet the conditions work separately. Any ideas?

while ((! file[$line_of_file]=~m/ENTRY=(.*)/) || ("$line_of_file" eq
"$total_number_of_lines_in_file"))
 
B

Ben Morrow

Quoth "EmJayEm said:
I have a text file with contents:

ENTRY= entry 1
here goes the contents for entry 1
here goes the contents for entry 1
here goes the contents for entry 1
ENTRY= entry 2
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2
ENTRY= entry 3
here goes the contents for entry 3
here goes the contents for entry 3
here goes the contents for entry 2

In the text file, entry 1 is on line 1, entry 2 is on line 5, entry 3 is on
line 9.

I wish to display just one entry. eg. entry 2, like so:
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2

I've done it like this:

# get the entry's line number in the text file

while (! file[$line_of_file]=~m/ENTRY=(.*)/) # for each line of the entry
^^ $

Please post real code. It is just a waste of everybody's time if you
post fake code. Have you read the posting guidelines?
until the next entry.
{
print file[$line_of_file]; # print the line of the entry
}

For printing entry 3 the condition in the while loop has to test the end of
the file, like so:

while ("$line_of_file" eq "$total_number_of_lines_in_file") # while not
end of file
{
print file[$line_of_file]; # print the line of the entry
}

I've tried to combine the two condition statements into one while loop as
below but it didn't work, yet the conditions work separately. Any ideas?

while ((! file[$line_of_file]=~m/ENTRY=(.*)/) || ("$line_of_file" eq
"$total_number_of_lines_in_file"))

I think you want to look at the .. operator in scalar context. Try
something like this:

my $entry = 'entry 1';

for (@file) {
print if (/ENTRY= $entry/ ... /ENTRY=/) > 1;
}

See perldoc perlop.

Ben
 
J

John W. Krahn

EmJayEm said:
I have a text file with contents:

ENTRY= entry 1
here goes the contents for entry 1
here goes the contents for entry 1
here goes the contents for entry 1
ENTRY= entry 2
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2
ENTRY= entry 3
here goes the contents for entry 3
here goes the contents for entry 3
here goes the contents for entry 2

In the text file, entry 1 is on line 1, entry 2 is on line 5, entry 3 is on
line 9.

I wish to display just one entry. eg. entry 2, like so:
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2

my $entry = qr/\bentry 2\b/;

while ( <FILE> ) {
if ( /^ENTRY=(?=.*?$entry)/ ... /^ENTRY=/ and !/^ENTRY=/ ) {
print;
}
}



John
 
M

Matt Garrish

EmJayEm said:
comments please.

I like it!

If you mean explanation, that's usually left to the reader. Take a look at
perlre if you don't understand regular expressions and perlop if the '...'
has you confused.

Matt
 
U

Uri Guttman

E> comments please.

it's intuitively obvious!

perldoc perlop and look for range

perldoc perlre and look for lookahead.

the best comments are in the docs.

uri
 
G

Gunnar Hjalmarsson

EmJayEm said:
I have a text file with contents:

ENTRY= entry 1
here goes the contents for entry 1
here goes the contents for entry 1
here goes the contents for entry 1
ENTRY= entry 2
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2
ENTRY= entry 3
here goes the contents for entry 3
here goes the contents for entry 3
here goes the contents for entry 2

In the text file, entry 1 is on line 1, entry 2 is on line 5,
entry 3 is on line 9.

I wish to display just one entry. eg. entry 2, like so:
here goes the contents for entry 2
here goes the contents for entry 2
here goes the contents for entry 2

A variant, playing with the input record separator (see "perldoc
perlvar"):

my $entry = qr/^\s*entry 2\b/;
{
local $/ = 'ENTRY=';
while ( <FILE> ) {
if (/$entry/) {
print /.+\n([\S\s]+?)(?:ENTRY|$)/;
last;
}
}
}
 
E

EmJayEm

while (! file[$line_of_file]=~m/ENTRY=(.*)/) # for each line of the
entry
^^ $

Please post real code. It is just a waste of everybody's time if you
post fake code. Have you read the posting guidelines?

Sorry, that should have been @file
 
M

Michele Dondi

Wow ... what a great solution to the OP's problem! IMNSHO YMMV HAND :)

Personally I find Ben's solution more interesting and intuitively
simpler in that it uses ...'s return value (to exclude one endpoint),
which is something I had forgotten about!! Only it must be slightly
modified in order to exclude the other endpoint as of the OP's
request:

while (<FILE>) {
my $t = /ENTRY= entry 2/ ... /ENTRY=/;
last if $t =~ /E0$/;
print if $t>1;
}


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,801
Messages
2,569,658
Members
45,422
Latest member
KerryFalco

Latest Threads

Top