2015-06-02 15 views
0

我想知道如何最好地解析ldif文件(和類似ldif的文件),以便我可以將每個DN條目及其關聯屬性導入變量,而無需跨越到其他DN及其屬性,因爲所有內容都位於單個文件中。Bash如何從基於dn的ldif類型文件中選擇多行

請問這怎麼辦?

感謝您幫助noob out。

編輯: 一個例子LDIF樣的文件看起來是這樣的:

dn: cn=admins,cn=groups,cn=accounts,dc=mydom,dc=com 
    Group name: admins 
    Description: Account administrators group 
    GID: 721800000 
    Member users: admin, user2, user1 
    ipauniqueid: 2dafa3a2-b903-11e2-8a28-525400a60ac3 
    objectclass: top, groupofnames, posixgroup, ipausergroup, ipaobject, nestedGroup 

    dn: cn=editors,cn=groups,cn=accounts,dc=mydom,dc=com 
    Group name: editors 
    Description: Limited admins who can edit other users 
    GID: 721800002 
    Member users: user1 
    ipauniqueid: 2dc4446a-b903-11e2-a2fa-525400a60ac3 
    objectclass: top, groupofnames, posixgroup, ipausergroup, ipaobject, nestedGroup 

    dn: cn=employees,cn=groups,cn=accounts,dc=mydom,dc=com 
    Group name: employees 
    Description: Default group for all Qrios employees 
    GID: 721800006 
    Member users: user2, user3 
    ipauniqueid: 134ae6e0-b910-11e2-a7f3-525400a60ac3 
    objectclass: top, groupofnames, nestedgroup, ipausergroup, ipaobject, posixgroup 

我希望能夠選擇文件的部分,基於第一關鍵字(DN),並導入將這些行的值轉換爲變量,以便我可以使用它們,然後轉到下一節。

+1

恐怕你需要更具體些。通過示例輸入和預期輸出更新您的問題,以及您嘗試解決問題以及解決問題的方法,我們很樂意提供幫助!祝你好運! –

+0

謝謝@FredrikPihl。我仍然試圖將自己的頭圍繞在需要做的事情上,我真的不確定。 – Sina

回答

0

sina,我正在使用LDIF格式相當多,而bash只是不削減它。 我會強烈建議你開始使用Perl或Python各自的LDAP模塊:

其LDAP模塊的perl的只是一個小例子:

# Read in the LDIF file specified in $input_file 
my $ldif = Net::LDAP::LDIF->new($input_file, "r", onerror => 'warn', change => 1); 
# 
# Process the LDIF input file 
# 
while($entry = $ldif->read()) 
{ 
     # Get the Member attribute 
     my @mbr = $entry->get_value('Member'); 
     foreach my $value (@mbr) 
     { 
       # Here you have a 'Member' value in $value, do what you want 
     } 
} 

正如你所看到的,這使得事情變得非常簡單。此外,這些模塊考慮了LDIF中的所有不同約定,如縮寫行,變更類型等。

+0

非常感謝。讚賞。 – Sina

相關問題