The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by topwoman]

I am writing a script which searches for a string in a file. When it
encounters the string, it prints it. Then, the script must back up 4 lines,
and print the fifth line PRIOR to the target line. Anybody out there can tell
me how this is done in perl?

In several other scripts, it would be more efficient to read from the end of
the file forward, also searching for string. What is the perl way of doing
this?
import-bot (20211) [Avatar] Offline
#2
Re: Reading backward through a file
[Originally posted by trelane]

> I am writing a script which searches for a string in a file. When it
> encounters the string, it prints it. Then, the script must back up 4 lines,
> and print the fifth line PRIOR to the target line. Anybody out there can tell
> me how this is done in perl?

The easiest way to do this is simply to keep a cache of the previous five lines

# create a cache
my @cache

while (<>smilie
{



# remove the first element if the cache is too big
unshift @cache if (@cache > 5)
}

> In several other scripts, it would be more efficient to read from the end of
> the file forward, also searching for string. What is the perl way of doing
> this?
import-bot (20211) [Avatar] Offline
#3
Re: Reading backward through a file
[Originally posted by trelane]

> I am writing a script which searches for a string in a file. When it
> encounters the string, it prints it. Then, the script must back up 4 lines,
> and print the fifth line PRIOR to the target line. Anybody out there can tell
> me how this is done in perl?

Let's try that again without mozilla submitting the message before I'd done
writing it.

The easiest way to do this is simply to keep a cache of the previous five lines

# create a cache
my @cache

# loop through the file on STDIN/named on the commadn line
while (<>smilie
{
# check to see if we found the text 'mystring'
if (m/mystring/)
{
# print this line
print $_;

# print the oldest element in our cache
# that's five lines old.
print $cache[0];
}

# put the current element at the end of the cache
push @cache, $_;

# remove the first element if the cache is too big
unshift @cache if (@cache > 4)
}

Note that I'm not sure what you mean when you say the fourth line prior.

LINE 1 <-- my script prints out this line
LINE 2
LINE 3
LINE 4
LINE WITH MATCHING STRING IN IT

Also you didn't say what you want to do if you found the string and you
hadn't read enough lines to go back five lines. In this case my script
will print out the first line of the script

> In several other scripts, it would be more efficient to read from the end of
> the file forward, also searching for string. What is the perl way of doing
> this?

This is actually quite hard to do. File systems are designed to read forwards
not backwards (due to the way hard disks and things do caching.) You can more
around in the file in perl using the 'seek' command so it is possible..just
very complicated. If this is a massive saving then it's probably easier to
use one of the existing utilities to reverse a file and then process that.

Have a look at section 8.4 of the Perl Cookbook that has things to say about
this.