2014-02-13 12 views
0

我有一個文件類似以下內容找到特定的標記,並取代所有領域下它在Perl

[amrit] 
type=friend 
host=111.118.253.145 
port=2776 
username=amrit 
secret=password 
disallow=all 
allow=gsm 
context=sip-calling 
qualify=yes 
call-limit=22 

[windwos] 
type=friend 
host=111.118.253.145 
port=2775 
username=amrit 
secret=password 
disallow=all 
allow=gsm 
context=sip-calling 
qualify=yes 
call-limit=50 


[windwos121254] 
type=friend 
host=111.118.253.145 
port=2776 
username=amrit 
secret=password 
disallow=all 
allow=ulaw 
allow=alaw 
context=sip-calling 
qualify=yes 
call-limit=99 

現在我想建立在Perl腳本。這會發現所有這個標籤下的[阿姆裏特]

type=friend 
host=111.118.253.145 
port=2776 
username=amrit 
secret=password 
disallow=all 
allow=gsm 
context=sip-calling 
qualify=yes 
call-limit=22 

現在我想更新的每個字段的值都行,但值應該是這個標籤的變化只是我一直在使用Perl腳本找到這些線,但我我無法改變這些檔案的價值。腳本如下

#!/usr/bin/perl 
$count = 0; 
open (IN, "file.txt"); 
while (<IN>) { 
    if (/\[amrit\]/) { 
    $count = 1; 
    } 
    elsif (/\[*\]/) { 
    $count = 0; 
    } 
    elsif ($count) { 
    print; 
     } 
} 
close IN; 

現在我想要更新[amrit]標記中每行的值。我是perl的初學者,請在這裏幫助我。

+4

設定值如果是一個INI文件,你可以使用['配置:: INI'(HTTP: //search.cpan.org/perldoc?Config%3A%3AINI)。 – TLP

+0

好吧,你所有的價值都要改變或者一些特殊的標籤? – JackXu

+0

@TLP它是一個conf文件 – user2916639

回答

1

Config::Tiny是在這裏爲你想要的東西。

#! /usr/bin/perl -w 

    use strict; 
    use Config::Tiny; 

    my $config = Config::Tiny->read('test.ini'); 

    my $section ="amrit"; 

    print "[$section]\n"; 
    foreach my $parameter (keys %{$config->{$section}}) 
    { 
     print "\t$parameter = $config->{$section}->{$parameter}\n"; 
    } 

這裏是讀取特定標籤數據的腳本。

您可以通過此

$Config->{section}->{username} = 'No user';  # Change a value 

而且更加有用的api如下

# Changing data 
    $Config->{newsection} = { this => 'that' }; # Add a section 
    $Config->{section}->{Foo} = 'Not Bar!';  # Change a value 
    delete $Config->{_};      # Delete a value or section 
+0

爲什麼你在這些部分上使用'for'循環?如果你刪除了'for'循環和'if'語句,並用'my $ section ='amrit'替換它們,'它會更快更清晰。 –

+0

我如何在沒有...循環的情況下獲取節名? –

+0

如果您知道麪糊解決方案,請提前編輯我的答案。 –

1

使用AppConfig

#!/usr/bin/perl 
use strict; 
use warnings; 
use AppConfig qw/:argcount/; 
use Data::Dumper; 

my $config = AppConfig->new({ CREATE => 1, GLOBAL => { ARGCOUNT => ARGCOUNT_ONE } }); 
    $config->file(\*DATA); 

my %amrit = $config->varlist('^amrit_'); 
print Dumper \%amrit; 

print $config->get('amrit_type'), "\n"; 
     $config->set('amrit_type', 'enemy'); 
print $config->get('amrit_type'), "\n"; 

__DATA__ 
[amrit] 
type=friend 
host=111.118.253.145 
port=2776 
username=amrit 
secret=password 
disallow=all 
allow=gsm 
context=sip-calling 
qualify=yes 
call-limit=22 

[windwos] 
type=friend 
host=111.118.253.145 
port=2775 
username=amrit 
secret=password 
disallow=all 
allow=gsm 
context=sip-calling 
qualify=yes 
call-limit=50 


[windwos121254] 
type=friend 
host=111.118.253.145 
port=2776 
username=amrit 
secret=password 
disallow=all 
allow=ulaw 
allow=alaw 
context=sip-calling 
qualify=yes 
call-limit=99 
+0

如果您需要寫入配置文件本身,雖然可能,但使用AppConfig很煩瑣。如TLP所建議的Config :: INI將是優選的。 – Chris