Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-26-2002, 12:25 PM   #1
Senior Member
Registered: Aug 2002
Location: Stuttgart (Germany)
Distribution: Debian/GNU Linux
Posts: 1,467

Rep: Reputation: 46
perl: replacing a special line in a file

I'm wondering if there is a easier solution to replace 1 line in a file ... this script just should change the /etc/login.defs ... any ideas?


# change min lengh of passwords
    open(FHANDLE, "/etc/login.defs");
    @file_content = <FHANDLE>;
    for ($i = 0; $i < scalar(@file_content); $i++) {
        if (!(@file_content[$i] =~ /^#/) && (@file_content[$i] =~ /PASS_MIN_LEN/)) {
            $file_content[$i] = "PASS_MIN_LEN\t8\n";
            last; }   

    open(FHANDLE, ">/etc/login.defs");
    print FHANDLE @file_content;    
Old 09-26-2002, 02:05 PM   #2
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
I'd do this :


$input_file = "/etc/login.defs";
open(FIN, $input_file);
open(FOUT, ">$input_file");

while(<FIN>) {
   next if m/^#/;
   print FOUT "PASS_MIN_LEN\t8\n" and last if m/PASS_MIN_LEN/;
untested, but you get the idea.

Last edited by lackluster; 09-26-2002 at 02:06 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM
PERL editing single line in file with regex indiescene Programming 0 04-14-2004 08:18 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 01:07 PM
Replacing PHP with Perl in Apache? miknight Programming 1 12-20-2002 01:32 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration