Waqas Ali Razzaq Waqas Ali Razzaq - 6 months ago 14
Bash Question

Read lines from text file based on last match of particular string using groovy or shell script

I have a text file from which I need to read the lines based on last matching condition. e.g read all the line till the end of file after last occurrence of specific

word
or
string
.

Sample file:

2016 Jun 01 13:48:46:590 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300006 Engine COMPLEXITY_CALCULATOR-GenerateComplexitySheet terminating
2016 Jun 01 13:50:47:692 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300001 Process Engine version 5.11.0, build V62_hotfix017, 2015-9-24
2016 Jun 01 13:50:47:702 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300009 BW Plugins: version 5.11.0, build V62_hotfix017, 2015-9-24
2016 Jun 01 13:48:46:590 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300006 Engine COMPLEXITY_CALCULATOR-GenerateComplexitySheet terminating
2016 Jun 01 13:50:47:692 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300001 Process Engine version 5.11.0, build V62_hotfix017, 2015-9-24
2016 Jun 01 13:50:47:702 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300009 BW Plugins: version 5.11.0, build V62_hotfix017, 2015-9-24
2016 Jun 01 13:50:47:710 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300011 Java version: Java HotSpot(TM) 64-Bit Server VM 23.3-b01
2016 Jun 01 13:50:47:711 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300012 OS version: amd64 Linux 2.6.32-573.3.1.el6.x86_64
2016 Jun 01 13:50:51:776 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300002 Engine COMPLEXITY_CALCULATOR-GenerateComplexitySheet started


From above file I want to read all the lines after
last occurrence
of the string
COMPLEXITY_CALCULATOR-GenerateComplexitySheet terminating


Expected Output:

2016 Jun 01 13:50:47:692 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300001 Process Engine version 5.11.0, build V62_hotfix017, 2015-9-24
2016 Jun 01 13:50:47:702 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300009 BW Plugins: version 5.11.0, build V62_hotfix017, 2015-9-24
2016 Jun 01 13:50:47:710 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300011 Java version: Java HotSpot(TM) 64-Bit Server VM 23.3-b01
2016 Jun 01 13:50:47:711 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300012 OS version: amd64 Linux 2.6.32-573.3.1.el6.x86_64
2016 Jun 01 13:50:51:776 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300002 Engine COMPLEXITY_CALCULATOR-GenerateComplexitySheet started

Answer

Try this :-

def file = new File("file.txt")
def index = file.findLastIndexOf {it =~ "COMPLEXITY_CALCULATOR-GenerateComplexitySheet terminating" }
def lines = file.readLines()
lines[(index+1)..(lines.size()-1)].each { println it }

Output :-

2016 Jun 01 13:50:47:692 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300001 Process Engine version 5.11.0, build V62_hotfix017, 2015-9-24 
2016 Jun 01 13:50:47:702 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300009 BW Plugins: version 5.11.0, build V62_hotfix017, 2015-9-24 
2016 Jun 01 13:50:47:710 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300011 Java version: Java HotSpot(TM) 64-Bit Server VM 23.3-b01 
2016 Jun 01 13:50:47:711 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300012 OS version: amd64 Linux 2.6.32-573.3.1.el6.x86_64  
2016 Jun 01 13:50:51:776 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300002 Engine COM PLEXITY_CALCULATOR-GenerateComplexitySheet started

Hope it will help you..:)

Comments