LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-26-2002, 12:25 PM   #1
markus1982
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?

Code:
#!/usr/bin/perl

# change min lengh of passwords
    open(FHANDLE, "/etc/login.defs");
    @file_content = <FHANDLE>;
    close(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;    
    close(FHANDLE);
 
Old 09-26-2002, 02:05 PM   #2
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
I'd do this :

Code:
#!/usr/bin/perl

$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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

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