grepを使用してtnsnames.oraを解析し、ホスト名からドメインを抽出します
質問
次のようなtnsnames.oraファイルがあります
DB_CONNECTION1=
(description=
(address=
(protocol=tcp)
(host=myhost1.mydomain.com)
(port=1234)
)
(connect_data=
(sid=ABCD)
(sdu=4321)
)
DB_CONNECTION2=
(description=
(address=
(protocol=tcp)
(host=myhost2.mydomain.com)
(port=1234)
)
(connect_data=
(sid=ABCD)
(sdu=4321)
)
キーホストから値myhostを抽出するために使用する必要のある正規表現。
Ouput should be
myhost1
myhost2
解決
grepは行全体を出力するため、次のような手順で実行できます。
grep "(host=" tnsnames.ora | cut -f 2 -d '=' | cut -f 1 -d '.'
分類するには:
- grep"(host =" tnsnames.ora-エントリ"(host =" を持つすべての行を検索
- cut -f 2 -d '='-'='文字で除算すると、列番号2にあるものを検索します
- cut -f 1 -d '。' -「。」で除算すると、列番号1にあるものを検索します文字
コマンドチェーンを任意のポイントまで実行して、次のような中間結果を確認できます。
grep "(host=" tnsnames.ora | cut -f 2 -d '='
次のものを提供します:
myhost1.mydomain.com)
myhost2.mydomain.com)
この方法で、この種のことを行うためのコマンドセットを簡単に構築できます。
所属していません StackOverflow