How to edit some lines of an existing file in Perl?

Posted on 2012-08-17
Last Modified: 2012-08-21
This is the code I tried to use to open and edit some matching lines of an existing file. But it looks like, it does not work.

Can you please let me know what I am doing wrong?

open my $TEMPFILE, "+<$file" 
                      or die "Couldn't open temp file: $! \n Check if the temp file is created.";	
	while (<$TEMPFILE>){
		s{ \w+ , \s \w+ \s+ \d+ , \s 20\d\d \s+ \d\d : \d\d : \d\d }{Weekday, Month DD, 20YY  HH:MM:SS}xmsg;
		s{ / 20 \d\d \d\d \d\d _  \d\d \d\d }{/20YYMMDD_HHMM}xmsg;
close $TEMPFILE;

Open in new window

Question by:Tolgar
    LVL 26

    Expert Comment

    Short answer: You can't directly, see:

    Solution: either write each read and / or manipulated line to a temporary file, then truncare and rename the files at the end, alternatively call sed.
    LVL 28

    Accepted Solution

    You could edit the file by using: Tie::File - Access the lines of a disk file via a Perl array

    To be more specific, this is what I'm suggesting.
    tie my @tempfile, 'Tie::File', $file or die "failed to tie '$file' $!";
    foreach ( @tempfile ) {
        s{ \w+ , \s \w+ \s+ \d+ , \s 20\d\d \s+ \d\d : \d\d : \d\d }{Weekday, Month DD, 20YY  HH:MM:SS}xmsg;
        s{ / 20 \d\d \d\d \d\d _  \d\d \d\d }{/20YYMMDD_HHMM}xmsg;
    untie @tempfile;

    Open in new window

    LVL 84

    Assisted Solution

    # or
    {local @ARGV=($file); local $^I=".bak";

          while ( <> ){
                s{ \w+ , \s \w+ \s+ \d+ , \s 20\d\d \s+ \d\d : \d\d : \d\d }{Weekday, Month DD, 20YY  HH:MM:SS}xmsg;
                s{ / 20 \d\d \d\d \d\d _  \d\d \d\d }{/20YYMMDD_HHMM}xmsg;

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now