freelanceprogrammers.org Forum Index » Perl
substituting text in a file
Joined: 05 Jun 2004
Posts: 2
substituting text in a file
I am a perl newbie and need to do text substitution in a file. I want
to do that without having to open two files-one for reading the
contents and another for writing the modified text.
Is there a way for perl to parse the file line by line ,do the
substitutions in some of the lines as it parses, and save the same
file with the modifications?
thanks
Ranjan
Joined: 01 May 2004
Posts: 7
substituting text in a file
On Fri, 04 Jun 2004 21:48:13 -0000, linuxtyro <linuxtyro@...> wrote:
> I am a perl newbie and need to do text substitution in a file. I want
> to do that without having to open two files-one for reading the
> contents and another for writing the modified text.
> Is there a way for perl to parse the file line by line ,do the
> substitutions in some of the lines as it parses, and save the same
> file with the modifications?
Are you doing this on the commandline or on the web?
AFAIK, Perl parses the file line-by-line by default.
Anyway, what you probably want to do is to save the parsed contents of
the file to a BIG variable, close it, then write it to the same file.
Maybe something like this:
----
open($IN, "$file") or die "Can`t open $file for reading: $!";
while (!eof($file)) {
$text = <$IN>;
<code to modify $text>
$big_var .= "$text
";
}
close($IN);
open($OUT, ">$file") or die "Can`t open $file for writing: $!";
print $OUT "$big_var";
close($OUT);
----
There`s probably an easier way to do this, but that`s how i would do
what you want... unless i`ll just use it on the commandline. If that`s
the case, sorry, i forgot how to do it... try searching the list.
HTH
Joined: 05 May 2004
Posts: 16
substituting text in a file
If your substitution is not very complex (that is, it is just a single
s///), then the command line might be your best option.
Checkout `perldoc perlrun` for the full reference, but the options you
want to check are:
-e, of course, which executes the code following the flag
-i[*extension*], which defines the extension to add to the original
filename, that remains as a backup (in case your one-lines goes bad you
still have the original to work from)
-n
causes Perl to assume the following loop around your program, which
makes it iterate over filename arguments somewhat like sed -n or
awk:
LINE:
while (<>) {
... # your program goes here
}
Note that the lines are not printed by default. See -p to have
lines printed. If a file named by an argument cannot be opened for
some reason, Perl warns you about it and moves on to the next file.
-p
Same as -n but you do not need to specify print at a certain stage in
your loop. As you are only doing a single substitution, -p might be OK.
If you are working on several s/// steps, you might want to use -n, go
through all your substitutions and then print. See these examples from
the -i flag under perlrun:
----------
These sets of one-liners are equivalent:
$ perl -pi -e `s/bar/baz/` fileA # overwrite current
file
$ perl -pi `*` -e `s/bar/baz/` fileA # overwrite current
file
$ perl -pi `.orig` -e `s/bar/baz/` fileA # backup to
`fileA.orig`
$ perl -pi `*.orig` -e `s/bar/baz/` fileA # backup to
`fileA.orig`
----------
Play around the command line options without actually writing your
original file (do a `> filename` first) and see your results. Whan
you`re confortable with the results you can add the -i flag.
I know all of this options are included as part of the normal
scripting, that is, no need to do this on the command line, but I have
never used tham like that.
Good luck
--- eman <eman@...> wrote:
> On Fri, 04 Jun 2004 21:48:13 -0000, linuxtyro <linuxtyro@...>
> wrote:
> > I am a perl newbie and need to do text substitution in a file. I
> want
> > to do that without having to open two files-one for reading the
> > contents and another for writing the modified text.
> > Is there a way for perl to parse the file line by line ,do the
> > substitutions in some of the lines as it parses, and save the same
> > file with the modifications?
>
> Are you doing this on the commandline or on the web?
> AFAIK, Perl parses the file line-by-line by default.
> Anyway, what you probably want to do is to save the parsed contents
> of
> the file to a BIG variable, close it, then write it to the same file.
> Maybe something like this:
> ----
> open($IN, "$file") or die "Can`t open $file for reading: $!";
> while (!eof($file)) {
> $text = <$IN>;
> <code to modify $text>
> $big_var .= "$text
";
> }
> close($IN);
> open($OUT, ">$file") or die "Can`t open $file for writing: $!";
> print $OUT "$big_var";
> close($OUT);
> ----
> There`s probably an easier way to do this, but that`s how i would do
> what you want... unless i`ll just use it on the commandline. If
> that`s
> the case, sorry, i forgot how to do it... try searching the list.
> HTH
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~-->
> Yahoo! Domains - Claim yours for only $14.70
> http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/ndFolB/TM
>
--------------------------------------------------------------------~->
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







