Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-28-2011, 01:32 AM   #1
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
Perl script to replace a line after finding a previous line

Hi all,

At the moment I have a flat file which is being used by a few people. I want a script to remotely change the file, so I can start logging who is doing what.

At this point here is one requirement I am trying to develop. We have text blocks who pretty much look like this:

---------------------- C001


---------------------- C006


In this text block I first want to find C001 and afterwards change $var3. Thing is, I don't want to change all $var3's.

This is my code so far;

	open(PARAM, "<be_cycle3b.prm") or die ("Kan parameter bestand niet openen.");

	my @dat_param = <PARAM>;

	foreach my $regel (@dat_param){

		if( $regel =~ /^-+ +$workflow/g ){

			print $regel;

			foreach (my $regel_n = @dat_param) {

				print " \n regel_n: " , $regel_n  , " \n ";

				if( $regel_n =~ /^$param/ ){
					print " foreach regel_n: ",$param;

					print $regel_n;



I have not come to the point of changing $$var3. Even trying to read the line with $var3 is not possible yet.

Here are the used variables:

$workflow (C001 as in example given)

$param = $$var3

I hope this is somewhat clear. I try to find $param for the right $workflow and change that. Can you help me to find $$var3 and change that?

Much appreciated.
Old 02-28-2011, 05:09 PM   #2
LQ Newbie
Registered: Aug 2005
Distribution: Slackware 12.2
Posts: 13

Rep: Reputation: 0
There is more than one way to do it

My recommendation is to use a state structure, since this makes it fairly simple to see what is going on.

	open(PARAM, "<be_cycle3b.prm") or die ("Kan parameter bestand niet openen.");
	my @dat_param = <PARAM>;
        my $state = 0;
	foreach my $regel (@dat_param){
		if( $regel =~ m/^-+/ ){
                        if($regel =~ m/$workflow/){
                                #You found the right section
                                $state = 1;
                                #You are in a wrong section
                                $state = 0;
                if($state == 1 and $regel =~ m/^$param/){
                        # You have now read the right line,
                        # so do something useful below.
			print "$regel";
This way you don't end up processing the wrong lines.

As for how to change $var3, just remember that, if $var = 'a' you can access $a as $$var, but read the perlreftut and/or perlref manpages for the complete story.

Good luck


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace line with another line in shell script pramod.srk Linux - Newbie 5 02-26-2010 06:00 AM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:08 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration