The easiest way to modify a file in place is probably to use Perl's nifty in-place editing feature. Here's one way of doing it:
All you have to do is use the -i switch and put the name of the file(s) to change in @ARGV. Then you can just read in lines with <>, modify them, and print them back out.#!/usr/bin/perl -i.bak # in-place editing; save the original file with a .bak extension use Win32::Console; use strict; open(STDERR, ">error.log") or die "Can't open error log: $!\n"; my $con = Win32::Console->new(); $con->Mode(ENABLE_WINDOW_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); $con->Display; $con->Write("Please enter the agent ID number: "); chomp(my $port = <STDIN>); $con->Write("The port number to use is $port.\n"); #Debug stuff, this +works { local @ARGV = "c:\\blah.txt"; # set up a temporary @ARGV for in-place editing while (<>) { s/serverport 111$/serverport $port/; print; } }
See perlrun for more about Perl's -i command line switch.
In reply to Re: modifying a text file on Win32
by chipmunk
in thread modifying a text file on Win32
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |