INI 파일의 특정 섹션에서 여러 줄을 편집합니다.

INI 파일의 특정 섹션에서 여러 줄을 편집합니다.

Subversion 구성 파일이 있습니다 ( ~/.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위의 정규식으로 어떻게 작업할 수 있나요 ?

이 샘플 전복 파일은 다음과 같습니다.

### 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

관련 정보