我想将日志文件中的行复制到单独的文件中。它应该只复制那些包含给定列表中ip的行。
//编辑 - 更详细的测试代码 这是我的代码:
static String[] ips = [
"192.168.1.2",
"192.168.1.1"
];
public static void main(String[] args) {
AntBuilder ant = new AntBuilder();
String folder = 'C:/logs';
ips.each {
String ip = it;
File file = new File("${folder}/singular_incidents/${ip}.txt")
ant.copy(tofile:"${folder}/singular_incidents/${ip}.txt"){
fileset(dir: folder){
include(name:"*access*.log")
}
filterchain(){
linecontains(){
contains(ip)
}
}
}
}
}
AND日志文件(accesslog-2014-05-05.log)内容可能如下所示
192.168.1.1 - user1 [24/Apr/2014:15:51:06 +0200] 'GET HTTP/1.1'
192.168.1.1 - user1 [24/Apr/2014:15:51:38 +0200] 'POST '
192.168.1.2 - user2 [24/Apr/2014:15:51:40 +0200] 'POST '
192.168.1.3 - user3 [24/Apr/2014:16:60:40 +0200] 'POST '
目标是我应该获得两个带名字的单独文件 - 192.168.1.1.txt - 包含两个第一行 - 192.168.1.2.txt - 包含第三行
不幸的是,当使用linecontains时,我收到此错误(我已经混淆了实际的ip字符串):
Caught: class org.apache.tools.ant.filters.LineContains$Contains doesn't support nested text data ("555.555.555.555")
答案 0 :(得分:2)
希望这有助于你:
String[] ips = [
"192.168.1.2",
"192.168.1.1"
]
def ant = new AntBuilder()
def dest = new File('/tmp')
new File('.').eachFileMatch(~/access.*.log/) { log ->
ips.each { ip ->
File file = new File(dest, "${ip}.txt")
ant.copy(file: log.name, tofile: "$dest.absolutePath/${ip}.txt") {
filterchain() {
linecontains() {
contains(value: "$ip")
}
}
}
}
}