0

I want to match multiple patterns in text files on the same line, copy and modify the matched line. I want to duplicate every line that contains either the string "businessServices" or "BusinessServices". I don't care where the pattern to match appears in the line. I want to insert the copied lines at the end of each section (pattern). e.g. The string "import ... ;" indicated one section. i.e ";" indicated the end of that section. The string "{ ... }" indicated another section. i.e "}" indicated the end of that section. I want to edit files in place. The desired added line are these global substitutions:

s#businessServices#userServices#g
s#BusinessServices#UserServices#g

in:

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) {
}

Desired:

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;
}

I tried the following awk script. The result is very close, except some line breaks disappear. How to preserve the line breaks in the input file?

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

Actual Output (some line breaks disappear):

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;
}
3
  • This is very similar to your last question. Is this something you need to do a lot? If not, you're probably better off just using a decent text editor and doing a yank-paste-edit (because the amount of effort to write and re-write the awk script greatly exceeds the benefit). If it is, you're probably off generating the entire file from a template, or separating the parts that can be auto-generated into another file that can be #included.
    – cas
    Dec 28, 2021 at 22:38
  • My text editors are Eclipse, Notepad++ and Visual Studio Code, which I do yank-paste-edit. I am exploring the awk or sed script to find out how much effort I can save. I plan to use the awk or sed script first, then, if needed, edit the files to get the desired result.
    – albertkao9
    Dec 29, 2021 at 1:46
  • Thanks for your comments. I edited to clarify my post.
    – albertkao9
    Dec 29, 2021 at 14:12

1 Answer 1

1

I don't understand the logic about when to print what blocks of modified lines (in your example sometimes it's immediately after the block, sometimes it's after a subsequent block or maybe at the end of file) and the output for the block that's not inside a function isn't what you want but here's a start that shows how to work on functions that in combination with your original script gives you what you need to build on:

$ 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) {
}
2
  • Ed Morton, Thanks for your comments. I changed my awk script. The result is very close, except some line breaks disappear. How to preserve the line breaks in the input file?
    – albertkao9
    Dec 31, 2021 at 16:33
  • I'm sorry, I won't be able to support the iterative development of your code. Also, please don't change your question after you have answers, always just accept an answer to the question you asked if you get one, then ask a new question if you have to.
    – Ed Morton
    Dec 31, 2021 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .