동일한 줄에 있는 텍스트 파일의 여러 패턴을 일치시키고 일치하는 줄을 복사하고 수정하고 싶습니다. "businessServices" 또는 "BusinessServices" 문자열이 포함된 모든 행을 복사하고 싶습니다. 일치시키려는 패턴이 줄의 어디에 나타나는지는 상관하지 않습니다. 각 섹션(패턴) 끝에 복사된 행을 삽입하고 싶습니다. 예를 들어 "import ... ;"라는 문자열은 섹션을 나타냅니다. IE ";"는 이 섹션의 끝을 나타냅니다. 문자열 "{ ... }"는 또 다른 부분을 나타냅니다. 즉, "}"는 이 부분의 끝을 의미합니다. 파일을 그 자리에서 편집하고 싶습니다. 추가해야 할 행은 다음과 같은 전역 대체입니다.
s#businessServices#userServices#g
s#BusinessServices#UserServices#g
존재하다:
import com.my.ClientBusinessServicesRequest;
import com.my.ClientBusinessServicesResponse;
import com.my.ClientOtherServicesRequest;
import com.my.ClientOtherServicesResponse;
ClientBusinessServicesResponse callClientBusinessServices(String businessServicesRequestKey) {
ClientBusinessServicesRequest clientBusinessServicesRequest = new ClientBusinessServicesRequest();
ClientBusinessServicesResponse clientBusinessServicesResponse = new ClientBusinessServicesResponse();
clientBusinessServicesRequest.setKey(businessServicesRequestKey);
clientBusinessServicesResponse = callClientBusinessServicesInternal(clientBusinessServicesRequest);
return clientBusinessServicesResponse;
}
ClientOtherServicesResponse callClientOtherServices(String otherServicesRequestKey) {
}
예상하다:
import com.my.ClientBusinessServicesRequest;
import com.my.ClientBusinessServicesResponse;
import com.my.ClientOtherServicesRequest;
import com.my.ClientOtherServicesResponse;
import com.my.ClientUserServicesRequest;
import com.my.ClientUserServicesResponse;
ClientBusinessServicesResponse callClientBusinessServices(String businessServicesRequestKey) {
ClientBusinessServicesRequest clientBusinessServicesRequest = new ClientBusinessServicesRequest();
ClientBusinessServicesResponse clientBusinessServicesResponse = new ClientBusinessServicesResponse();
clientBusinessServicesRequest.setKey(businessServicesRequestKey);
clientBusinessServicesResponse = callClientBusinessServicesInternal(clientBusinessServicesRequest);
return clientBusinessServicesResponse;
}
ClientOtherServicesResponse callClientOtherServices(String otherServicesRequestKey) {
}
ClientUserServicesResponse callClientUserServices(String userServicesRequestKey) {
ClientUserServicesRequest clientUserServicesRequest = new ClientUserServicesRequest();
ClientUserServicesResponse clientUserServicesResponse = new ClientUserServicesResponse();
clientUserServicesRequest.setKey(userServicesRequestKey);
clientUserServicesResponse = callClientUserServicesInternal(clientUserServicesRequest);
return clientUserServicesResponse;
}
다음 awk 스크립트를 시도했습니다. 일부 개행 문자가 누락된 점을 제외하면 결과는 매우 유사합니다. 입력 파일에서 줄 바꿈을 유지하는 방법은 무엇입니까?
awk -i inplace '
/import.*;/ {
origimportbuf = origimportbuf $0 ORS
}
/import.*usinessServices.*;/ {
importbuf = importbuf $0 ORS
}
/Services.*{/,/}/ {
origbuf = origbuf $0 ORS
}
/usinessServices.*{/,/}/ {
buf = buf $0 ORS
}
END {
printf "%s", origimportbuf
gsub(/BusinessServices/,"UserServices",importbuf)
gsub(/businessServices/,"userServices",importbuf)
printf "%s", importbuf
printf "%s", origbuf
gsub(/BusinessServices/,"UserServices",buf)
gsub(/businessServices/,"userServices",buf)
printf "%s", buf
};
' in.txt
실제 출력(일부 줄 바꿈이 사라짐):
import com.my.ClientBusinessServicesRequest;
import com.my.ClientBusinessServicesResponse;
import com.my.ClientOtherServicesRequest;
import com.my.ClientOtherServicesResponse;
import com.my.ClientUserServicesRequest;
import com.my.ClientUserServicesResponse;
ClientBusinessServicesResponse callClientBusinessServices(String businessServicesRequestKey) {
ClientBusinessServicesRequest clientBusinessServicesRequest = new ClientBusinessServicesRequest();
ClientBusinessServicesResponse clientBusinessServicesResponse = new ClientBusinessServicesResponse();
clientBusinessServicesRequest.setKey(businessServicesRequestKey);
clientBusinessServicesResponse = callClientBusinessServicesInternal(clientBusinessServicesRequest);
return clientBusinessServicesResponse;
}
ClientOtherServicesResponse callClientOtherServices(String otherServicesRequestKey) {
}
ClientUserServicesResponse callClientUserServices(String userServicesRequestKey) {
ClientUserServicesRequest clientUserServicesRequest = new ClientUserServicesRequest();
ClientUserServicesResponse clientUserServicesResponse = new ClientUserServicesResponse();
clientUserServicesRequest.setKey(userServicesRequestKey);
clientUserServicesResponse = callClientUserServicesInternal(clientUserServicesRequest);
return clientUserServicesResponse;
}
답변1
수정된 줄의 어떤 블록을 인쇄해야 하는지(귀하의 예에서는 때로는 블록 바로 뒤, 때로는 후속 블록 뒤 또는 파일 끝에 있을 수도 있음)와 그렇지 않은 블록의 출력을 언제 인쇄해야 하는지에 대한 논리를 이해하지 못합니다. 함수 내부는 여러분이 원하는 것이 아니지만, 다음은 빌드에 필요한 것을 제공하기 위해 원래 스크립트와 결합된 함수를 사용하는 방법을 보여주는 시작입니다.
$ cat tst.awk
/{$/ { inFunc = 1 }
{ buf = buf $0 ORS }
inFunc && /^}/ { inFunc = 0 }
!NF && !inFunc {
orig = buf
printf "%s", buf
gsub(/BusinessServices/,"UserServices",buf)
gsub(/businessServices/,"userServices",buf)
if ( buf != orig ) {
printf "%s", buf
}
buf = ""
}
$ awk -f tst.awk file
import com.my.ClientBusinessServicesRequest;
import com.my.ClientBusinessServicesResponse;
import com.my.ClientOtherServicesRequest;
import com.my.ClientOtherServicesResponse;
import com.my.ClientUserServicesRequest;
import com.my.ClientUserServicesResponse;
import com.my.ClientOtherServicesRequest;
import com.my.ClientOtherServicesResponse;
ClientBusinessServicesResponse callClientBusinessServices(String businessServicesRequestKey) {
ClientBusinessServicesRequest clientBusinessServicesRequest = new ClientBusinessServicesRequest();
ClientBusinessServicesResponse clientBusinessServicesResponse = new ClientBusinessServicesResponse();
clientBusinessServicesRequest.setKey(businessServicesRequestKey);
clientBusinessServicesResponse = callClientBusinessServicesInternal(clientBusinessServicesRequest);
return clientBusinessServicesResponse;
}
ClientUserServicesResponse callClientUserServices(String userServicesRequestKey) {
ClientUserServicesRequest clientUserServicesRequest = new ClientUserServicesRequest();
ClientUserServicesResponse clientUserServicesResponse = new ClientUserServicesResponse();
clientUserServicesRequest.setKey(userServicesRequestKey);
clientUserServicesResponse = callClientUserServicesInternal(clientUserServicesRequest);
return clientUserServicesResponse;
}
ClientOtherServicesResponse callClientOtherServices(String otherServicesRequestKey) {
}