Hey all I have a pretty simple question. I am trying to replace a line in a file that is being processed.
See the code below for a better understanding.
#!/usr/bin/perl
while (my $line=<>) {
my ($blah,$blah2,$bla,$time,$blah3)=split(/:/,$line);
if ($blah eq "test") {
$time="UNDEF";
# Replace line in same file.
}
}
I know, a very simple example, but I think it gets the idea across. Rather than opening and writing to a new file, I just want to replace the line in the file that we are working with. I want to update the $time lets say. See below for a BEFORE/AFTER example.
BEFORE: "test:bob:jones:04/20/09:john",
AFTER: "test:bob:jones:UNDEF:john"
UPDATE: I am not looking to do this for a file at command line. I am looking to do this within my code for any file given to the script. I can do it with a system call to sed (i.e., system("sed \"s/$line/$blah:$blah2:$bla:$time:$blah3/\" FILE > /tmp/file; mv /tmp/file FILE); ), however, that is not something that I wish to do, and I know that there is a far easier way to do this without system calls.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.