使用groovy或shell脚本根据特定字符串的最后一次匹配从文本文件中读取行
我有一个文本文件,我需要根据上一个匹配条件从中读取行。e、 g在上次出现特定的使用groovy或shell脚本根据特定字符串的最后一次匹配从文本文件中读取行,shell,groovy,sh,Shell,Groovy,Sh,我有一个文本文件,我需要根据上一个匹配条件从中读取行。e、 g在上次出现特定的单词或字符串后,读取所有行,直到文件末尾 示例文件: 2016 Jun 01 13:48:46:590 GMT +0200 BW.COMPLEXITY_CALCULATOR-GenerateComplexitySheet Info [BW-Core] BWENGINE-300006 Engine COMPLEXITY_CALCULATOR-GenerateComplexitySheet terminating 201
单词
或字符串
后,读取所有行,直到文件末尾
示例文件:
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
从上面的文件中,我想读取字符串最后一次出现后的所有行
预期产出:
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
既然您在groovy标记上发布了这个,我希望您可以从shell中使用groovy。我写了下面的脚本,效果很好。虽然它会在文件中迭代两次,但它将以流式方式工作,不会占用您的内存:
f = new File("sample.txt")
def lastIndex
f.eachLine { line, index ->
if (line.contains("GenerateComplexitySheet terminating")) {
lastIndex = index + 1
}
}
new File("out.txt").with {
write ""
withWriter { writer ->
f.eachLine { line, index ->
if (index >= lastIndex) {
writer.writeLine line
}
}
}
assert text == '''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
'''
}
既然您在groovy标记上发布了这个,我希望您可以从shell中使用groovy。我写了下面的脚本,效果很好。虽然它会在文件中迭代两次,但它将以流式方式工作,不会占用您的内存:
f = new File("sample.txt")
def lastIndex
f.eachLine { line, index ->
if (line.contains("GenerateComplexitySheet terminating")) {
lastIndex = index + 1
}
}
new File("out.txt").with {
write ""
withWriter { writer ->
f.eachLine { line, index ->
if (index >= lastIndex) {
writer.writeLine line
}
}
}
assert text == '''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
'''
}
试试这个:-
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 }
输出:-
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
希望它能帮助你……:)试试这个:-
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 }
输出:-
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
希望它能帮助您:)无论使用何种语言,都有两种算法可以实现这一点:
第一:
initialise a temporary store (memory or temp file)
open input
while(read line) {
if(line matches search pattern) {
clear temp store
}
write line to temp store
}
copy temp store to output
第二:
open input
while(read line) {
if(line matches search pattern) {
store line number in variable
}
}
close input
open input again
read until stored line number
read / write until end
第一个选项的优点是,它与管道输入一起工作,您无法在开始时重新打开输入。但它的缺点是,在到达输入的最后一行之前,必须将输出行存储在临时位置
第二个选项的优点是,它一次只能在内存中保存一行输入。它的缺点是,它永远无法使用无法从一开始就重新打开的输入源
您应该能够相当容易地在Groovy或shell中实现这两种语言中的任何一种
在shell中,如果输入是文件,则可以拼凑第二种算法的版本:
tail --lines=+$(grep -n pattern input.txt | tail -1 | cut -d: -f1) input.txt
在这里,我们使用grep-n
查找匹配行(带有行号),tail-1
选择最后一行,cut
提取行号,tail--lines=+n
将这些行写入标准输出。无论使用何种语言,都有两种算法可以实现这一点:
第一:
initialise a temporary store (memory or temp file)
open input
while(read line) {
if(line matches search pattern) {
clear temp store
}
write line to temp store
}
copy temp store to output
第二:
open input
while(read line) {
if(line matches search pattern) {
store line number in variable
}
}
close input
open input again
read until stored line number
read / write until end
第一个选项的优点是,它与管道输入一起工作,您无法在开始时重新打开输入。但它的缺点是,在到达输入的最后一行之前,必须将输出行存储在临时位置
第二个选项的优点是,它一次只能在内存中保存一行输入。它的缺点是,它永远无法使用无法从一开始就重新打开的输入源
您应该能够相当容易地在Groovy或shell中实现这两种语言中的任何一种
在shell中,如果输入是文件,则可以拼凑第二种算法的版本:
tail --lines=+$(grep -n pattern input.txt | tail -1 | cut -d: -f1) input.txt
在这里,我们使用grep-n
查找匹配的行(带有行号),tail-1
选择最后一行,cut
提取行号,以及tail--lines=+n
将这些行写入标准输出。你能给我们看看你的代码和你得到的错误吗?我正在尝试:“tail-n”+$(($(grep-n'COMPLEXITY\u CALCULATOR-GenerateComplexitySheet终止'COMPLEXITY\u CALCULATOR-GenerateComplexitySheet.log | head-n1 | cut-d:“-f1)+1”)“COMPLEXITY\u CALCULATOR-GenerateComplexitySheet.log”但它是从第一次匹配返回的。你能给我们看看你的代码和你得到的错误吗?我正在尝试这个:“tail-n”+$(($(grep-n'COMPLEXITY_CALCULATOR-GenerateComplexitySheet终止'COMPLEXITY_CALCULATOR-GenerateComplexitySheet.log | head-n1 | cut-d:“-f1)+1”)“COMPLEXITY_CALCULATOR-GenerateComplexitySheet.log”但它从第一次匹配返回。