Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find / replace vaules in a file

Status
Not open for further replies.

newperluser

Programmer
Feb 11, 2007
3
US
I'm new to PERL so please forgive me if this seem like a silly question..

I need to open a file and parse throgh it replacing one value with another

I'm thinking something like this sould work .. but it does not

if (open TESTFILE, "testfile.txt")
{
while(<TESTFILE>)
if (/oldvalue/)
{
s/oldvalue/newvalue/;
}

}
close TESTFILE;

I'm sure I am missing something simple - but I'm not seeing it.

Thanks
 
Code:
open(TESTFILE, "<testfile.txt");
open(OUTFILE, ">outfile.txt");
while (<TESTFILE>) {
 $_ =~ s/oldvalue/newvalue/g;
 print OUTFILE "$_";
}
close TESTFILE;
close OUTFILE;

Not tested or validated. (I'm just gonna add that to my sig :) )
 
newperluser, in the sampwo mistakes. First the g flag of the reg exp - without it you will replace only the first ocurrence of the expression. Second you don't anything with the replaced string - may be prin/store it somewhere - it is replaced only in the $_ variable, but not in the file.

Corwin
 
you have opened a file for reading, which is the default mode when you do this:

Code:
open TESTFILE, "testfile.txt")

so trying to write to it will not work. But it would still not work even if you used write mode:

Code:
open TESTFILE, "[red]>[/red]testfile.txt")

because that is also over-write/create mode, so any existing file would be over-written or created if it did not exist.

Using read/write mode is buggy at best but you could try it:

Code:
open TESTFILE, "[red]+<[/red]testfile.txt")

The best way to do what you are trying is to use perls inplace editor


Code:
[gray]#!/usr/bin/perl [/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red] 
[black][b]use[/b][/black] [green]warnings[/green][red];[/red] 

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$filename[/blue] = [red]'[/red][purple]some_file.txt[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$find[/blue] = [red]'[/red][purple]this[/purple][red]'[/red][red];[/red] 
[black][b]my[/b][/black] [blue]$replace[/blue] = [red]'[/red][purple]that[/purple][red]'[/red][red];[/red] 

[red]{[/red]
   [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]@ARGV[/blue] = [red]([/red][blue]$filename[/blue][red])[/red][red];[/red] 
   [black][b]local[/b][/black] [blue]$^I[/blue] = [red]'[/red][purple].bac[/purple][red]'[/red][red];[/red] 
   [olive][b]while[/b][/olive][red]([/red] <> [red])[/red][red]{[/red] 
      [olive][b]if[/b][/olive][red]([/red] [red]s/[/red][purple][blue]$find[/blue][/purple][red]/[/red][purple][blue]$replace[/blue][/purple][red]/[/red][red]ig[/red] [red])[/red] [red]{[/red] 
         [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url][red];[/red] 
      [red]}[/red] 
      [olive][b]else[/b][/olive] [red]{[/red] 
         [black][b]print[/b][/black][red];[/red] 
      [red]}[/red] 
   [red]}[/red] 
[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple]Finished[/purple][red]"[/red][red];[/red]


see FAQ faq219-6549 for explanations of the above code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top