CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
How do I modify the line after a matched line?

 



perl_oyster
New User

Sep 17, 2011, 7:32 PM

Post #1 of 6 (6348 views)
How do I modify the line after a matched line? Can't Post

Hi Guys,

I have a file and I am searching for a line with a pattern. I have got that working. However I need to substitute a word on the line immediately following the line that matched. Right now I am trying to read the entire file in a list and then using @list[i+1] in a loop to modify the line immediately following the line that matched. Is there an easier way to do this? Any line offsets that can be used?

Thanks.


wickedxter
User

Sep 18, 2011, 11:37 AM

Post #2 of 6 (6322 views)
Re: [perl_oyster] How do I modify the line after a matched line? [In reply to] Can't Post

plz post code of what you have


perl_oyster
New User

Sep 18, 2011, 10:44 PM

Post #3 of 6 (6294 views)
Re: [wickedxter] How do I modify the line after a matched line? [In reply to] Can't Post

I am trying something like this:


Code
#!/usr/bin/perl 
use strict;
use warnings;

print "Starting script\n\n";

my $file_name = "test.txt";

my $pos = 0;

open (FILE, $file_name) or die ("File could not be found!!");

while (<FILE>) {

if (/pattern/) {

$pos = tell(FILE);
$line = $.;
# Get length of current line in bytes (characters)
$length_of_line = #Need help here to calculate this
#Calculate offset
$offset = $length_of_line + $pos;
#Now go to next line (offset from current location)
seek(FILE, $offset, 1) or die "Couldn't seek to $offset: $!\n";

#Now perform the substitution

} else {next};
}

print "End script\n";

close(FILE);


I am trying to avoid lists. Is this doable?


(This post was edited by perl_oyster on Sep 18, 2011, 10:45 PM)


FishMonger
Veteran / Moderator

Sep 19, 2011, 8:37 AM

Post #4 of 6 (6250 views)
Re: [perl_oyster] How do I modify the line after a matched line? [In reply to] Can't Post

http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm


perl_oyster
New User

Sep 29, 2011, 6:06 PM

Post #5 of 6 (5690 views)
Re: [FishMonger] How do I modify the line after a matched line? [In reply to] Can't Post

Is there any other way to do this (possibly on the same lines of what I posted above)other than using CPAN? I want to avoid CPAN because of version compatibility issues.


FishMonger
Veteran / Moderator

Sep 30, 2011, 6:24 AM

Post #6 of 6 (5680 views)
Re: [perl_oyster] How do I modify the line after a matched line? [In reply to] Can't Post

What compatibility issues are you worried about?

File::Find is one of the core modules that comes with Perl, so it won't have any compatibility issues.

There are several ways to accomplish what you need without the module. Since you didn't provide complete/proper specs, I can't guaranty this will do what you want, but should be very close.


Code
#!/usr/bin/perl 

use strict;
use warnings;

print "Starting script\n\n";

my $file_name = "test.txt";
open my $fh, '<', $file_name or die "could not open '$file_name' $!";

while ( my $line = <$fh> ) {
if ( $line =~ /your pattern/ ) {
print $line;
my $next_line = <$fh>;
$next_line =~ s/change this/to something/;
print $next_line;
}
else {
print $line;
}
}
close $fh;



(This post was edited by FishMonger on Sep 30, 2011, 6:24 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives