# Question about range of Lines

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Given this simple file:

cat t1

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

with

perl -ne 'print if /Tuesday/ ..  /Friday/;' t1

I can get:

Tuesday
Wednesday
Thursday
Friday

Is there a simple way to modify the range selection in order to get all
the lines but the last in the range?
Namely, getting this output:

Tuesday
Wednesday
Thursday

An idea could be to push back last line but I do not know how to do that.

Thank you

Regards

Mauro

PS this is an oversimplified case for the sake of simplicity

## Re: Question about range of Lines

The Thu, 15 Sep 2011 09:28:42 +0200, mauro papandrea wrote :

You mean, something like this ?

perl -ne 'while(<>) { push @a, \$_ if /Tuesday/ ..  /Friday/; } pop @a;
print @a' < t1

## Re: Question about range of Lines

On 15/09/2011 14.17, lcof wrote:

Thank you

However it is not so clear how it works; I thought that -n option was
supposed to add an implicit loop and there were no need to add a further

Regards

Mauro

## Re: Question about range of Lines

Mauro wrote :

Sure, I did not took off the -n option after copy-pasting your line, so I
used a redirection. Consider the following :

perl -e 'while(<>) { push @a, \$_ if /Tuesday/../Friday/ } pop @a; print
@a' t1

Good luck with your algorithm. Btw Tad McClellan's solution is much more
efficient.

--
perl -e 's;;`_^[&)/#%(`&;;y;%^)([]/*#&`_{};.
0acghiklmopsz;;print'

## Re: Question about range of Lines

Well, now it is much clearer; just wondering why this do not work:

perl -ne '{ push @a, \$_ if /Tuesday/../Friday/ } pop @a; print @a' t1

Regards

Mauro

## Re: Question about range of Lines

Mauro wrote :

This gives :

while(<>) {
{
push @a, \$_ if /Tuesday/../Friday/
}
pop @a;
print @a
}

That's not what you want. You have to use an END block to get the pop and
print instructions out of the while loop, like this :

perl -ne 'push @a, \$_ if /Tuesday/../Friday/; END { pop @a; print @a }' t1

{ push @a, \$_ if /Tuesday/../Friday/ } pop @a; print @a

--
perl -e 's;;`_^[&)/#%(`&;;\
y;%^)([]/*#&`_{};.0acghiklmopsz;;print'

## Re: Question about range of Lines

On 15/09/2011 16.54, Lucien Coffe wrote:

Oh, I see, those two {} more ... that is the difference, I missed that

Thank you for pointing out it to me

Regards

Mauro

## Re: Question about range of Lines

perl -ne '\$a = 1 if /Tuesday/; \$a = 0 if /Friday/; print if \$a' t1

--
email: perl -le "print scalar reverse qq/moc.liamg0cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.

## Re: Question about range of Lines

On 15/09/2011 14.34, Tad McClellan wrote:

Thanl you

This appears much clearer, I hope to be able to adapt it to solve my
original problem, that was slightly different ( I am afraid I
oversimplified too much ).

Regards

## Re: Question about range of Lines

Another solution which avoids the range operator:

perl -e '\$/ = undef; \$s = <>; print \$s =~ /^(Tuesday.*?)^Friday/sm;' t1

hp

## Re: Question about range of Lines

Thank you, it is a nice idea ( but for too much long files, perhaps ).

Regards

Mauro

## Re: Question about range of Lines

"but for too much long files, perhaps"

I would give this credibility if you can show some code that
guarantees there is a 'Friday'.

while (<>) {
if (/Tuesday/ .. /Friday/) {}
}

In this case, the operator .. flips true on \$_=~/Tuesday/ and remains
so until after it finds \$_=~/Friday/ or, until the while() is false.

I can see why you would be interested in how the range operator
works in this case, but don't promote it to something perfect.

-sln

## Re: Question about range of Lines

On 19/09/2011 21.39, sln@netherlands.com wrote:

I had expressed that doubt ( about long files ) since, if I understand
correctly, that solution read the whole file in memory.
The example I posted was an artificial oversimplified case, just to have
something concrete to apply code, not the real case ( where there is no
friday at all ).

Thank you

Regards

Mauro

## Re: Question about range of Lines

Yes, it does. Whether that's a problem depends on how long the file is,
how much memory you have and what you are doing with the file. Caveat
emptor!

hp

## Re: Question about range of Lines

True. My code behaves differently than the original if the Friday is
missing. But maybe a range which doesn't end with Friday is malformed
and should be omitted. Mauro didn't specify, but this is definitely
something he would have to consider.

I was assuming that "Tuesday" and "Friday" are just arbitrary begin and
end markers and that lines don't have a natural order. But what if they
have? Given the input:

Monday
Wednesday
Thursday
Friday
Saturday
Sunday

should it print

Wednesday
Thursday

?

Then neither the range operator nor the regexp will work (both rely on
"Tuesday" being there) and you would need a different approach (like
converting weekdays to numbers and then checking whether the number is
in [2, 5)).

hp

## Re: Question about range of Lines

On 25/09/2011 8.42, Peter J. Holzer wrote:

Your assumption is quite correct, those day name were merely markers.
Perhaps I oversimplified too much.
In my original problem, my file is divided in sections and each of them
begins with a special line ( easily identifiable because no other line
can be like that, that file is produced by a tool ).
I need to read each section and apply a kind of filter whose action
depends upon the type of section ( the marker line has 2 parts: one
equal for every section, and this can be used to find sections, and a
second with a kind of label, that can be used to understand which
section it is ), some are to be discarded and others to be changed.
So I though that a line range was good way to start if I could omit last
line in the range ( so that it could be used for next section ) but I am
afraid I did not chose the right example and I apologize for that.

Regards

Mauro

## Re: Question about range of Lines

mauro papandrea wrote:

\$ echo "Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday" | perl -ne'print if (/Tuesday/ ..  /Friday/) && !/Friday/'
Tuesday
Wednesday
Thursday

John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

## Re: Question about range of Lines

On 16/09/2011 2.36, John W. Krahn wrote:

Thank you very much

Regards

Mauro

## Re: Question about range of Lines

perl -ne 'print if \$r=3D/Tuesday/../Friday/
and \$r !~ /E/'

See explanation of "Range Operators"
in:  perldoc perlop

--
Charles DeRykus

## Re: Question about range of Lines

Thank you very much, I had missed the fact that range operator ..
appends "E0" to the end-of-range line so that it can be used in a test.

Regards

Mauro