Closed Thread Icon

Preserved Topic: Perl technique question (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=20912" title="Pages that link to Preserved Topic: Perl technique question (Page 1 of 1)" rel="nofollow" >Preserved Topic: Perl technique question <span class="small">(Page 1 of 1)</span>\

 
Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-16-2001 01:09

OK. Let's say I want to take a file containing many logs, such as this:

May 13, 2001

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 05-16-2001 01:24

1) open existing file for read
2) open new file for write
3) do your thing, 1 line at a time
4) rename new file to old file, and old file to something else.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-16-2001 01:41

That's smart. How can I rename and delete files?

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 05-16-2001 02:24

rename() and unlink() should work quite nicely, although I don't have the manpages in front of me. WarMage?

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 05-16-2001 05:00

Yeah... max would know better...

Just start at root and use the command ulink(*.*), wait don't do that... I was just kidding.

So yeah that should do what you are looking for, have fun with it.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-16-2001 08:52

As Linear has already said pretty much everything I can only add usage instructions for those two functions...

rename OLDNAME,NEWNAME

Changes the name of a file; an existing file NEWNAME will be clobbered. Returns true for success, false otherwise.
Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this. Other restrictions include whether it works on directories, open files, or pre-existing files.

unlink LIST
unlink


Deletes a list of files. Returns the number of files successfully deleted.

$cnt = unlink 'a', 'b', 'c';
unlink @goners;
unlink <*.bak>;

Note: unlink will not delete directories unless you are superuser and the -U flag is supplied to Perl. Even if these conditions are met, be warned that unlinking a directory can inflict damage on your filesystem. Use rmdir instead.

If LIST is omitted, uses $_.

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 05-16-2001 16:39

It's probably also worth dsiscussing how $

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 05-16-2001 16:46

Slime: "And I can't read from the file while simultaneously writing to the file."

Actually, in Perl you can. Either call Perl with the -i flag or set $^I = '.bak' in your script. The original file will land in foo.bak and the edited file will replace the original file.

This is the answer I should have issued first, because it's much more idiomatic to Perl. Refer to p. 332 of the Camel book.

« BackwardsOnwards »

Show Forum Drop Down Menu