-E를 사용하고 여러 -e를 사용하지 않을 때 GNU sed에 여러 표현식을 제공하는 방법은 무엇입니까?

-E를 사용하고 여러 -e를 사용하지 않을 때 GNU sed에 여러 표현식을 제공하는 방법은 무엇입니까?

다음과 같은 파일이 있습니다 bitcoin.conf.

#Bind to given address and always listen on it (comment out when not using as a wallet):
#bind=127.0.0.1
daemon=1
#debug=i2p
debug=tor
#Pre-generate this many public/private key pairs, so wallet backups will be valid for
# both prior transactions and several dozen future transactions
keypool=2
#i2pacceptincoming=1

#i2psam=127.0.0.1:8060
#Make outgoing connections only to .onion addresses. Incoming connections are not affected by this option:
onlynet=i2p
onlynet=onion
#running Bitcoin Core behind a Tor proxy i.e. SOCKS5 proxy:
proxy=127.0.0.1:9050

다음 질문에 대한 답변을 조사한 후이것은 물어볼 가치가 있는 질문이다#, 몇 줄의 주석을 달도록 설계된 스크립트로 솔루션을 구현하려고 합니다 .

#!/usr/bin/bash

#Semicolons do not seem to work below.
#This gives me no error but it does not do anything 
#sed -Ee 's/^\(debug=tor\)/#\\1/;s/^\(onlynet=onion\)/#\\1/;s/^\(proxy=127\)/#\\1/' bitcoin.conf

#This works fine
sed -Ee s/^\(debug=tor\)/#\\1/ -e s/^\(onlynet=onion\)/#\\1/ -e s/^\(proxy=127\)/#\\1/ bitcoin.conf 

#When grouping, and therefore extended regexp, is not applied semicolons seem to work fine
sed -e 's/^debug=tor/#debug=tor/;s/^onlynet=onion/#onlynet=onion/;s/^proxy=127/#proxy=127/' bitcoin.conf 


##This gives me no error but it doesn't do anything
#sed -Ee 's/^\(debug=tor\)/#\\1/
#  s/^\(onlynet=onion\)/#\\1/ 
#  s/^\(proxy=127\)/#\\1/' bitcoin.conf 




#sed -i  -E -e 's/^\(debug=tor\)/#\\1/' -e 's/^\(onlynet=onion\)/#\\1/' -e 's/^\(proxy=127\)/#\\1/' \
#-e 's/^\(addnode\)/#\\1/' -e 's/^\(seednode\)/#\\1/' bitcoin.conf #does not comment out anything

-EGNU에 여러 표현식을 입력할 때 세미콜론과 개행 문자가 작동하지 않는 이유는 무엇입니까 sed?

sedPS 작품의 방식이 다르기 때문에 -E이 질문은 위에서 언급한 질문과 중복된 질문은 아닌 것 같습니다.

답변1

문제는 확장 기능을 활성화하기 위해 플래그를 추가 ERE하지만 BRE구문을 사용한다는 것입니다. 플래그를 제거하더라도 반환 참조도 이스케이프하므로 해당 줄은 1.

$ sed -Ee 's/^\(debug=tor\)/#\\1/;s/^\(onlynet=onion\)/#\\1/;s/^\(proxy=127\)/#\\1/'

위의 코드는 대괄호를 이스케이프하여 리터럴로 만듭니다. 일치하는 항목이 없으므로 아무것도 변경되지 않았습니다.

$ sed -Ee s/^\(debug=tor\)/#\\1/ -e s/^\(onlynet=onion\)/#\\1/ -e s/^\(proxy=127\)/#\\1/

에 의해 추가됨에드 모튼- 스크립트를 인용하지 않음으로써(나쁜 생각입니다!) sed가 보기 전에 해석을 위해 쉘에 노출되므로 쉘은 모든 선행 백슬래시를 소비하므로 sed가 보는 것은s/^(debug=tor)/#\1/ -e s/^(onlynet=onion)/#\1/ -e s/^(proxy=127)/#\1/

귀하의 질문에 대답하기 위해 여러 명령에 세미콜론을 사용하면 -E첫 번째 시도에 표시된 것처럼 플래그와 함께 작동하지만 일치하는 항목이 없습니다. 다음은 실제 예입니다.

$ sed -E 's/^(debug=tor)/#\1/;s/^(onlynet=onion)/#\1/;s/^(proxy=127)/#\1/'

&그러나 제외된 항목이 없으므로 반환 일치를 사용할 수도 있으므로 그룹 일치가 중복됩니다. 역참조나 메타문자를 사용하지 않기 때문에 실제로 확장 기능이 필요하지 않습니다.

$ sed 's/^debug=tor/#&/;s/^onlynet=onion/#&/;s/^proxy=127/#&/'

아니면 더 좋을 수도 있습니다 - 님이 추가함에드 모튼

$ sed -E 's/^(debug=tor|onlynet=onion|proxy=127)$/#&/'

답변2

HatLess는 귀하의 접근 방식이 작동하지 않는 이유를 이미 설명했지만 특정 문자열로 시작하는 줄에만 주석을 달고 싶다면 다음 구문을 사용할 수 있습니다.

sed '/^string/s/old/new/' file

이는 "이 줄이 로 시작하면 로 string바꾸십시오 . 따라서 명령을 다음과 같이 작성할 수 있습니다.oldnew

sed -E '/^(debug=tor|onlynet=onion|proxy=127)/s/^/#/' file

관련 정보