1

ファイルservices- 次のような多くのレコードが含まれています。

define service {
    host_name\t\t\t\tHOSTNAME
    ...
    ...
}

ファイルhosts- レコードが含まれています:

define host {
    host_name\t\t\t\tHOSTNAME
    ...
    ...
}

に移動してhosts、最初のレコードから HOSTNAME の名前を取得し、次にファイルに移動しservicesて、その HOSTNAME を持つすべてのレコードを検索し、それらを他のファイルに配置する必要があります。次に、 内のすべての HOSTNAME に対して実行しhostsます。

私が知らないのは、主にファイル ホストから HOSTNAME を取得する方法と、ファイル サービスのレコード全体を変数に取得する方法です。正規表現を用意しました(正しいことを願っています)^define.*host_name\t\t\t\t$HOSTNAME.*}

望ましい結果を得る方法について、いくつかのアドバイスや例を教えてください。

4

2 に答える 2

3

The files you provide look very much like nagios configuration files.

sed might be your friend here, as it allows you to slice the file into smaller parts, eg:

:t
/^define service {/,/}$/ {    # For each line between these block markers..
   /}$/!{         #   If we are not at the /end/ marker
      $!{          #     nor the last line of the file,
         N;        #     add the Next line to the pattern space
         bt
      }            #   branch (loop back) to the :t label.
   }               # This line matches the /end/ marker.
   /host_name[ \t]\+HOSTNAME\b/!d;       # delete the block if wrong host.
}

That example lifted from the sed faq 4.21, and adapted slightly. You could also look at question 4.22 which appears to address this directly:

http://sed.sourceforge.net/sedfaq4.html#s4.22

Like the previous answer, I'm also inclined to say you're probably better off using another scripting language. If you need a different interpreter to get this done anyway, might as well use something you know.

于 2013-04-19T12:01:15.130 に答える