How can I find a window of text in a file using Perl?
I'm trying to find a window of text within a file using Perl, but I'm not having much luck figuring it out. I've done it with the regular shell and Grep, but in this situation I must use Perl.
The basic idea is to pass a search term to the script, along with a window size, and have the script return the search term from within the file(s), along with a "window" of text surrounding the data. I.E. ./windowSearch -w4 filename(s) searchterm Results would be something like: preceding line preceding line searchterm subsequent line subsequent line This wasn't too hard with the shell, as shown below, but I need to do this with Perl. This is my original script Code:
#!/bin/bash |
Quote:
First solve a simpler problem - open input file(s) and print its/their contents to STDOUT. For example, start from here: http://www.perlfect.com/articles/perlfile.shtml . Then choosing lines according to some criteria will be simple. ... And will 'grep' do what you need ? |
Well, here's the perldocs (with examples); specifically open().
http://perldoc.perl.org/functions/open.html |
Okay, I've made some progress. Here's my script to search a file for a term, but I'm at a loss as how to search for a window of text around the matching line.
Code:
############# |
Quote:
Have you read about Perl 'push' and 'shift functions ? Using the two will allow pretty straightforwardly to have the window if I understand correctly what you mean. By the way, your script is bad in a sense it is memory hungry - because of Code:
@array=<FILE>; # Fill the array Functionally you do not need that at all. |
Quote:
|
Quote:
Furthermore, explicit numeric indexes in Perl programs are rarely needed, and more often than not a solution without them exists - using numeric indexes it's not considered to be good style in Perl. That's why I'm pushing the OP towards queue/fifo, which for starters can be implemented through an Perl array holding a small line of numbers, using, say, 'push' and 'shift' operations. |
Quote:
@OP, you can use a counter to get next lines after your search pattern Code:
while(<>){ |
Quote:
As for Sergei's comment about the array, can I just call the file handle directly? I.E. Code:
open(FILE, $file); Secondly, Although I know conceptually what push and shift do (move data off a stack?) I've never used them. I don't understand what the code in the last post (by ghostdog74) is doing either - print if $count --> 0? What is it printing? You'll have to forgive my ignorance - I've barely used Perl at all, and most of my scripting has been basic things. The "window" I refer to means "find the matching line, then print the nth line above and below the matching line as well." I.E. if our match was found on line 10, and the window size was 1, the script would print lines 9, 10 and 11. |
Quote:
Quote:
Quote:
Code:
while (<$file_handle>) { Quote:
Code:
my @array = qw/one two three four/; |
Quote:
When you know how to do this, the solution to your complete problem will be obvious. |
All times are GMT -5. The time now is 05:28 PM. |