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 "{}". After the search strings are matched, extract their values enclosed within "" without "()". 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;
}
}
}}}}}
Expected output:
AAA retrieveA
BBB retrieveB
CCC
retrieveD
EEE retrieveE
FFFFF retrieveF
GGGGGG getG
HHHHHHH getH
I tried the solution from: Extract multiple strings from a multiple lines text file
awk -v OFS='\t' -F= '
/\{[[:blank:]]*$/ {++n}
NF==2 && /String | pac/ {
gsub(/^[[:blank:]]*("|new +)|[()";]+$/, "", $2)
if ($1 ~ / (server|pac)/)
col1[n] = $2
else if ($1 ~ / method/)
col2[n] = $2
}
END {
for (i=1; i<=n; ++i)
print col1[i], col2[i]
}' in
You may try a modified version of the previous awk script here.
Then use it as: