How to extract multiple strings from a multiple lines text file with these rules? The search strings are "String server", " pac " and "String method". They may or may not appear only once within the enclosing "{}". If the value of the search string "String server" is not enclosed within "", ignore its value. After the search strings are matched, extract their values enclosed within "" without "()". The value of the search string " pac " take precedence over the search string "String server" value. The value of either search string "String server" or " pac " appear only once - no duplication. Its value will appear before the value of the search string "String method". e.g. sample text file in:
{{{{{
public AResponse retrieveA(ARequest req){
String server = "AAA";
String method = "retrieveA()";
log.info(method,
server,
req);
return res;
}
public BResponse retrieveB(BRequest req){
String method = "retrieveB()";
BBB pac = new BBB();
log.info(method,
pac,
req);
return res;
}
public CResponse retrieveC(CRequest req) {
String server = "CCC";
log.info(server,
req);
return res;
}
public DResponse retrieveD(DRequest req) {
String method = "retrieveD()";
log.info(method,req);
return res;
}
public EResponse retrieveE(ERequest req){
EEE pac = new EEE();
String method = "retrieveE()";
String server = "EEE";
log.info(method,
server,
pac,
req);
return res;
}
public FResponse callretrieveF(FRequest req) throws InvalidDataException {
String server = "FFFFF";
//retrieveF
String method = "retrieveF()";
try {
log.info(method,
server,
req);
FFFFF pac = new FFFFF();
}
}
/**
* callgetG
* getG
*/
public GResponse callgetG(GRequest req) throws InvalidDataException {
//getG
String method = "getG()";
String server = "GGGGGG";
try {
try {
GGGGGG pac = new GGGGGG();
log.info(method,
server,
req);
}
}
}
/**
* getH
*/
public HResponse getH(HRequest req)
throws InvalidDataException {
//getH
String method = "getH()";
String server = "HHHHHHH";
String calledMethod = "getH2()";
ARequest aReq = new ARequest(req.getH(),
req.getR());
ProgramAccountInformationResponse resp = null;
try {
log.info(LogMessages.msgInfoMethodStartPrivate(method,
server,
calledMethod,
req));
return resp;
}catch(InvalidDataException ide){
log.error(method);
throw ide;
}
}
public IResponse determineI(IRequest req){
String method = "determineI()";
}
private IResponse callI(IRequest req){
String method = "callI()";
IIIII pac = new IIIII();
String server = pac.getName();
}
}}}}}
Expected output:
AAA retrieveA
BBB retrieveB
CCC
retrieveD
EEE retrieveE
FFFFF retrieveF
GGGGGG getG
HHHHHHH getH
determineI
IIIII callI
The solution from Extract multiple strings from a multiple lines text file - larger test file do not get the last 2 output properly: determineI IIIII callI
You may use this refactored awk script for this data. Note that we need to check for both
privateorpublickeywords for our start of the block marker. Moreover checks like!col1[n]and!col2[n]are required to avoid overwriting same array indices for both the arrays.Then call this script as: