![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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.
|
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|