INIファイルの特定のセクションからいくつかの行を編集する

INIファイルの特定のセクションからいくつかの行を編集する

私はサブバージョンの設定ファイルを持っています(~/.subversion/servers

プロキシ情報(ホスト、ポート、例外)を追加するために変更する必要があります。このファイルにはプロキシ情報を含む多くのセクションが含まれています。変更したいのは[グローバル]

すでにこれに対する正規表現を作成しましたが、機能しません。

/(\[global\].*[\n])((.*[\n])*)([\s\#]*http-proxy-port\s?=\s?.*)/gm

オンラインでテストを試すことができます出典:https://regex101.com/次のように置き換えるとうまく機能します:

\1\2http-proxy-port=9000

sed上記の行を実行してみますが、何も表示されません。

sed -i -r 's/(\[global].*[\n])((.*[\n])*)([\s\#]*http-proxy-port\s?=\s?.*)/\1\2http-proxy-port=9000/gm' \
 ~/.subversion/servers

sed上記の正規表現を機能させるにはどうすればよいですか?

このサンプル Subversion ファイル:

### The currently defined server options are:
###   http-proxy-host            Proxy host for HTTP connection
###   http-proxy-port            Port number of proxy host service
###   http-proxy-username        Username for auth to proxy service
###   http-proxy-password        Password for auth to proxy service
###   http-proxy-exceptions      List of sites that do not use proxy
###   http-timeout               Timeout for HTTP requests in seconds

[groups]
# group1 = *.collab.net
# othergroup = repository.blarggitywhoomph.com
# thirdgroup = *.example.com

### Information for the first group:
# [group1]
# http-proxy-host = proxy1.some-domain-name.com
# http-proxy-port = 80
# http-proxy-username = blah
# http-proxy-password = doubleblah
# http-timeout = 60

### Information for the second group:
# [othergroup]
# http-proxy-host = proxy2.some-domain-name.com
# http-proxy-port = 9000

### SSL certificate.  See details above for overriding security
### due to SSL.
[global]
# http-proxy-exceptions = *.domain.org, *.domain.com
# http-proxy-host = proxy.domain.com
# http-proxy-port = 8080
# http-proxy-username = defaultusername
# http-proxy-password = defaultpassword

期待される出力は

...
[global]
http-proxy-exceptions = *.otherdomain.org, *.otherdomain.com, 127.0.0.1, localhost
http-proxy-host = proxy.otherdomain.com
http-proxy-port = 9000
# http-proxy-username = defaultusername
# http-proxy-password = defaultpassword

答え1

提案されたように、INIファイルを編集するより良い方法があります...
それでも、次の方法がありますsed:

sed '/^\[.*\]/h
/http-proxy-exceptions/{x;/\[global\]/!{x;b;};x;c\
http-proxy-exceptions = *.otherdomain.org, *.otherdomain.com, 127.0.0.1, localhost
}
/http-proxy-host/{x;/\[global\]/!{x;b;};x;c\
http-proxy-host = proxy.otherdomain.com
}
/http-proxy-port/{x;/\[global\]/!{x;b;};x;c\
http-proxy-port = 9000
}' infile

これは、一致する行に遭遇するたびに、ホールドバッファをパターンスペースの内容で上書きします[.*](つまり、各セクション名を古いバッファに保存します)。パターンhに一致する各行で、バッファが変更されます - ホールドスペースがhttp-.*xない( !) が一致する[global]と、 e がx戻って、 を介して次のサイクルにスキップしますb。ホールド スペースが一致すると、 [global]e がx戻って、cパターン スペースの内容をハングします。

関連情報