The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to move file pointer to a particular line in c user_prady High Level Programming 7 03-27-2008 12:32 AM
return the previous line user_prady Shell Programming and Scripting 12 12-18-2007 07:37 AM
how to go previous line in vim useless79 UNIX for Advanced & Expert Users 2 09-16-2007 09:10 AM
search and retrieve previous line in file paulsew Shell Programming and Scripting 2 02-23-2007 08:04 AM
Previous day's date in Perl? rajus19 Shell Programming and Scripting 4 07-18-2006 01:40 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 01-24-2008
tqlam tqlam is offline
Registered User
  
 

Join Date: Jul 2004
Posts: 12
Perl how to move pointer to previous line in a txt file?

I have a text file that has blocks of text. Each block starts with ### and ends with End_###.
I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block
if matched, need to print that whole block. According to the input file in below, it will print blocks #02, #04, #05.
Here is my question, I have no problems matching the SEARCH_STR, but once matched, how do I move back previous lines?
e.g. in block #02, matched in line 3, but I need to print lines 1 & 2.

Thanks in advance for any help!

MY SCRIPT:
=========
#!/46020/local/bin/perl -w
#
if ($#ARGV != 0) {
print "You must provide a search criteria.\n";
exit;
}
my $query = $ARGV[0];
my $INPUT = "/mypath/inputFILE.txt";
my $line;
my $tag = 0;
my @results = ();

open (FILE, "<$INPUT") or die "Cannot open '$INPUT' file: $!";
while ($line = <FILE>) {
next if ($line =~ /^#[0-9][0-9].* - / || $line =~ /^\s*$/);
if ($line =~ /$query/i) {
$tag = 1;
}
if ($line =~ /^End_#/ && $tag == 1) {
push (@results,$line) if ($tag == 1);
$tag = 0;
}
push (@results,$line) if ($tag == 1);
}
close (FILE);
print "@results\n";
exit;


INPUTFILE:
========
#01 - block start line here
some text here
more text here
more lines
more lines
more lines
End_#01

#02 - block start line here
some text here
this line contains the SEARCH_STR and something
more lines
more lines
End_#02

#03 - block start line here
some text here
more text here
more lines
more lines
more lines
End_#03

#04 - block start line here
some text here
more lines
more lines
the SEARCH_STR is here and something
some text here
more lines
more lines
more lines
End_#04

#05 - block start line here
some text here
more lines
the SEARCH_STR is here and something
more lines
End_#05


#06 - block start line here
some text here
more text here
more lines
more lines
more lines
End_#06
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 04:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0