同じ行にあるテキストファイルの複数のパターンを一致させ、一致する行をコピーして変更したいと思います。 「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) {
}