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 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-26-2008 09:32 PM
return the previous line user_prady Shell Programming and Scripting 12 12-18-2007 04:37 AM
how to go previous line in vim useless79 UNIX for Advanced & Expert Users 2 09-16-2007 05:10 AM
search and retrieve previous line in file paulsew Shell Programming and Scripting 2 02-23-2007 05:04 AM
Previous day's date in Perl? rajus19 Shell Programming and Scripting 4 07-17-2006 09:40 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-24-2008
Registered User
 

Join Date: Jul 2004
Posts: 9
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
Reply With Quote
Forum Sponsor
  #2  
Old 01-24-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Post

Try something like this:
Code:
#!/46020/local/bin/perl -w
$/="\n\n";
while (<STDIN>) { if (/$ARGV[0]/) { print $_ }}
./scriptname.pl SEARCH_STR < /mypath/inputFILE.txt
Reply With Quote
  #3  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
If Smiling_Dragons suggestion does not work, you can use the Tie::File module, which allows you to read and write to a file just like a perl array. See the Tie::File documentation for usage details. It's also a core module so you should have it installed already. If you get stuck, post back.
Reply With Quote
  #4  
Old 01-25-2008
Registered User
 

Join Date: Jul 2004
Posts: 9
Just want to clarify, according to the inputFILE.txt, when run the script with the SEARC_STR it will print #02, #04, #05 blocks. e.g. scriptname search.
My code able to print matched plus following lines up to End_###, but I need a way to print lines before matched in that block.

Thank you.
Reply With Quote
  #5  
Old 01-25-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
Yes, I think we both understood that requirement, and both suggestions can be used to do what you want. If you are familiar with looping through perl arrays using subscripts: $array[n] then using Tie::File will be pretty straight forward once you read the documentation.

S_M's suggestion reads the file in chunks, not line by line, and looks like it should work although the last match might fail if there isn't two newlines at the end of the file. You will have to try it and see or maybe he can clear that up.
Reply With Quote
  #6  
Old 01-27-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Quote:
Originally Posted by KevinADC View Post
S_M's suggestion reads the file in chunks, not line by line, and looks like it should work although the last match might fail if there isn't two newlines at the end of the file. You will have to try it and see or maybe he can clear that up.
I think it'll be ok with a normal file termination - I tried it on a single newline and it looked alright but I havn't put a lot of testing into it for sure
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0