Re: In-place file manipulation
by Corion (Patriarch) on Apr 30, 2003 at 15:03 UTC
|
The Tie::File module has an excellent way of treating a file like an array, and the synopsis provides all you need to know for basic usage :
# This file documents Tie::File version 0.93
tie @array, 'Tie::File', filename or die ...;
for (@array) {
s/PERL/Perl/g; # Replace PERL with Perl everywhere in th
+e file
}
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The
$d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider
($c = $d->accept())->get_request(); $c->send_response( new #in the
HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
| [reply] [d/l] [select] |
Re: In-place file manipulation
by broquaint (Abbot) on Apr 30, 2003 at 14:58 UTC
|
A sublime example for the use of the -i switch when using perl from the command line. See. perlrun for info on this marvellous switch (best used in conjunction with -p and -e).
HTH
_________ broquaint | [reply] |
|
I use this one all the time. A quick example of broquaint's suggested technique (from the command line), to replace all capitalization permutations of the word 'perl' to 'Perl' in file perl.pl, and store the original in perl.pl.bak:
perl -pi.bak -e 's/\bperl\b/Perl/gi' perl.pl
| [reply] [d/l] |
Re: In-place file manipulation
by halley (Prior) on Apr 30, 2003 at 15:53 UTC
|
The (-i) switch is so easy for adjusting TEXT files. I use this so often, that in my login script, I set an alias for pie=perl -p -i~ -e. Easy as pie.
However, -i really doesn't edit "in place," but it does a good simulation of it. It creates a new file and moves the old file to a backup extension. You may have problems with access control lists (ACLs) on some network file system implementations. Experiment.
The '+' modes for the open() function are designed for fixed-record DATA updating. Note both examples in (perldoc -f open) refer to database files. Not that you couldn't use the functions for text files, but it would be a horrible and unnecessary tangle of seek(), tell() and caching to avoid overwriting parts of the next line before it is read. Not to mention further complications for newline conversion entanglement.
I wish the docs were more clear about this, to avoid this recurring question.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] [select] |
Re: In-place file manipulation
by nite_man (Deacon) on Apr 30, 2003 at 15:39 UTC
|
use File::Data;
use strict;
use File::Data;
my $o_dat = File::Data->new('./t/example');
$o_dat->write("complete file contents\n");
$o_dat->prepend("first line\n"); # line 0
$o_dat->append("original second (last) line\n");
$o_dat->insert(2, "new second line\n"); # inc. zero!
$o_dat->replace('line', 'LINE');
print $o_dat->READ;
--------------------------------
SV* sv_bless(SV* sv, HV* stash);
| [reply] [d/l] |
Re: In-place file manipulation
by AcidHawk (Vicar) on Apr 30, 2003 at 15:01 UTC
|
| [reply] |
Re: In-place file manipulation- Played!
by benn (Vicar) on May 01, 2003 at 00:12 UTC
|
Surely - this was a reverse golf game? We start off well under par on the green with -i, through the rough with a couple of modules, back down the hill with an alias, round the 20-liner dog-leg and end up at the clubhouse with a dedicated however-many-thousand-line text editor solution. Good show! :) | [reply] |
Re: In-place file manipulation
by perlplexer (Hermit) on Apr 30, 2003 at 15:08 UTC
|
Do records in your file have the same length? If so, the best approach is to go with read/print/seek or sysread/syswrite/sysseek.
If record size may change after some of the fields in it are modified, you may find that having a temp file is easier than having to write a sub for shifting data within the file.
--perlplexer
| [reply] |
Re: In-place file manipulation
by bobdeath (Scribe) on Apr 30, 2003 at 14:58 UTC
|
I usually just read from the file, modify, and write to a new file, then override the first file with the second when I am done. Probably not the prettiest solution, but it gets the job done. | [reply] |
Re: In-place file manipulation
by atnonis (Monk) on Apr 30, 2003 at 16:22 UTC
|
Hello this is one way to do it!
it works for me
#!/usr/bin/perl -w
use strict;
die "usage: $0 <file> <search pattern> <replace pattern>",
unless @ARGV == 3;
my $searchFile = $ARGV[0];
my $searchPattern = $ARGV[1];
my $replacePattern = $ARGV[2];
my $tempFile = 'tmp$$';
open FILE, $searchFile #opens file in read-only mode
or die "Cannot open $searchFile in read-only mode. $!\n";
open TMP, '>'.$tempFile
or die "Cannot create temp file. $!\n";
while (<FILE>) {
chomp;
if (/$searchPattern/i) {
print "Match found...replacing at line $.\n";
$_ =~ s/$searchPattern/$replacePattern/i;
print TMP $_."\n";
}else {
print TMP $_."\n"
}
}
close FILE;
close TMP;
unlink $searchFile;
rename $tempFile, $searchFile;
Antonis! | [reply] [d/l] |
Re: In-place file manipulation
by LameNerd (Hermit) on Apr 30, 2003 at 16:30 UTC
|
Why not do something like this?
$ your_script < your_input > your_output
$ mv your_output your_input
| [reply] [d/l] |
|
Because I'm on Win2k :-{
Well, the real reason is that this isn't happening on the command line. But thanks anyway.
Cheers,
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
| [reply] [d/l] |
|
perl script.pl < source.txt > result.txt && move /Y result.txt source.
+txt
Where the && means "execute the second command only if the first one did not return an error"
Jenda
Always code as if the guy who ends up maintaining your code
will be a violent psychopath who knows where you live.
-- Rick Osborne
Edit by castaway: Closed small tag in signature | [reply] [d/l] [select] |
|
Can you get away with installing cygwin?
| [reply] |
Re: In-place file manipulation
by ibanix (Hermit) on Apr 30, 2003 at 21:15 UTC
|
After looking at the options...
I think I'm actually going to forget in-place manipulations on files and go directly to a module that will let me use a flat-file DB, and save myself the trouble. I'm just storing data -- the format won't matter greatly if I can read and store it consitently. I have not the need or the resources to run a full-scale SQL server.
So, any suggestions on a good flat-file DB module?
Thanks for everyone's suggestions!
Cheers,
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo; | [reply] [d/l] |
|
| [reply] |
|
I may be contradicting my previous post, but my problem is not your
problem. There is a whole class of software that does what you are
are trying to do. This class of software is called text editors.
If you have the where-with-all (I don't) you might want to look into
playing around with the source code of an open-source editors.
Also why just mess with a flat file DB when there are open-source
RDBMS you could use?
Good luck with your project!
| [reply] |