user2766909
user2766909

Reputation: 1

delete appended characters of line of INI file in perl

I am writing into an INI file, where the data gets appended and I want to delete remaining data.

Example: In INI file Data was: [email protected] I want to write [email protected] When I write it, it appends and makes it: [email protected]

I want to remove last characters(com which is rewritten). I am doing following:

#! /usr/bin/env perl

use strict;
use warnings;

seek(INI,-1,0);
print INI "[email protected]";

Upvotes: 0

Views: 82

Answers (3)

ikegami
ikegami

Reputation: 386331

Use the truncate function to move the end of the file.

By the way, seek(INI, -1, 0) (one byte before the start of the file) makes no sense. It should be seek(INI, 0, 0).

Upvotes: 1

psxls
psxls

Reputation: 6935

This should work:

#!/usr/bin/perl
use warnings;
use strict;
{
    local ($^I,@ARGV)=('~','file.ini');
    while (<>) {
        s/xyz/abc/;
        print;       
    }
}

Upvotes: 0

choroba
choroba

Reputation: 241968

If the records are not of the same length, this simple approach does not work. You cannot "insert" or "delete" bytes by overwriting a part of the file. You have to create a new file, write the new contents to it, and rename it to the old name at the end.

Upvotes: 0

Related Questions