匹配文本文件中的多个模式,复制并修改匹配的行

匹配文本文件中的多个模式,复制并修改匹配的行

我想在同一行上匹配文本文件中的多个模式,复制并修改匹配的行。我想复制包含字符串“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) {
}

相关内容