INIファイルの特定のセクションで数行を編集します。

INIファイルの特定のセクションで数行を編集します。

~/.subversion/serversSubversion設定ファイル()があります。

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

これに正規表現を作成しましたが、うまくいきません。

/(\[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上記の正規表現をどのように使用できますか?

この例では、ファイルを破壊します。

### 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]x再び変更され、次のループにスキップされますb。キープスペースが一致すると再び変更され、[global]パターンスペースの内容が一時停止します。xc

おすすめ記事