![]() |
|
|
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 |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|