我想从日志文件中获取最新版本的文件,并使用unix命令输出到新文件。
这是日志文件的一个示例。
patches.txt
Wed Jan 15 10:42:26 MYT 2014, Start, SPIRTE_4.3.2.B1, OK
Wed Jan 15 10:42:28 MYT 2014, End, TILES_7.3.66B1, DONE
Wed Jan 15 10:42:35 MYT 2014, End, MOVIES_OPENING_4.3.2.B1, FAILED
Wed Jan 15 10:42:36 MYT 2014, Start, MOVEMENT_7.3.68B1, OK
Wed Jan 15 10:42:36 MYT 2014, Start, MOBS_7.3.2B1, OK
Wed Jan 15 10:42:37 MYT 2014, End, TILES_7.3.68B1, DONE
Wed Jan 15 10:42:38 MYT 2014, Start, MOVIES_CUTSCENE_4.3.2.57B1, OK
Wed Jan 15 10:42:38 MYT 2014, Start, GAME_4.3.2.57B1_SERVER, OK
Wed Jan 15 10:42:43 MYT 2014, End, GAME_4.2.57B2_CLIENT, DONE
Wed Jan 15 10:42:43 MYT 2014, End, GAME_4.2.57B4_SERVER, DONE
Wed Jan 15 10:42:45 MYT 2014, End, GAME_4.2.57B1_CLIENT, DONE
Wed Jan 15 10:42:45 MYT 2014, End, GAME_4.2.57B1_SERVER, DONE
Wed Jan 15 10:42:45 MYT 2014, End, DB_4.2.57C2_CLIENT, DONE
Wed Jan 15 10:42:45 MYT 2014, End, DB_4.2.57C2_SERVER, DONE
1.文件的最新日期指定最新版本,例如
Wed Jan 15 10:42:26 MYT 2014, Start, SPIRTE_4.3.2.B1, DONE
Wed Jan 16 10:42:26 MYT 2014, Start, SPIRTE_4.3.2.B1, DONE //latest
Wed Jan 17 10:42:26 MYT 2014, Start, SPIRTE_4.3.2.B1, FAILED //does not consider
2.行末尾的DONE指定已安装补丁。
3.每个文件都有这种格式[characters _] [versionNumber _] [CLIENT / SERVER],[CLIENT / SERVER]不是必需的。
4.如果安装了多个版本的文件,最新版本将覆盖前一个版本。 e.g
Wed Jan 15 10:42:37 MYT 2014, End, TILES_7.3.68B1, DONE //overwritten
Wed Jan 16 10:45:37 MYT 2014, End, TILES_7.3.70C1, DONE //latest
5.后缀为_CLIENT的文件考虑使用后缀为_SERVER的不同文件,即使它们以相同的名称开头(但仍然属于规则#4)。 以此为例:
Wed Jan 15 10:39:23 MYT 2014, EndPatch, GAME_TILES_7_3_2_3B1_Client, DONE
Wed Jan 15 10:42:45 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.57B1, DONE
Wed Jan 15 10:41:39 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.42B4, DONE
Wed Jan 15 10:41:38 MYT 2014, EndPatch, GAME_TILES_7.3.2.33B3, DONE
Wed Jan 15 10:39:34 MYT 2014, EndPatch, GAME_TILES_7.3.2.1B1, DONE
Wed Jan 15 10:39:07 MYT 2014, EndPatch, GAME_TILES_7.3.2.ServicePackB1, DONE
GAME_TILES_7_3_2_3B1_Client
是一个文件。
GAME_TILES_7.3.2.33B3
是另一个档案。
GAME_TILES_7.3.2.1B1
与GAME_TILES_7.3.2.33B3
和GAME_TILES_7.3.2.ServicePackB1
例如输出文件
Wed Jan 15 10:42:37 MYT 2014, End, TILES_7.3.68B1, DONE
Wed Jan 15 10:42:45 MYT 2014, End, GAME_4.2.57B1_CLIENT, DONE
Wed Jan 15 10:42:45 MYT 2014, End, GAME_4.2.57B1_SERVER, DONE
Wed Jan 15 10:42:45 MYT 2014, End, DB_4.2.57C2_CLIENT, DONE
Wed Jan 15 10:42:45 MYT 2014, End, DB_4.2.57C2_SERVER, DONE
策略:
1. sort all the files
2. grep all the lines that has DONE
3. check for rule #4 and rule #5 (how? I can't use tail for this)
我现在的位置:grep'。* DONE'< patches.txt | sort
测试文件:
Wed Jan 15 10:36:16 MYT 2014, StartInstall, MOB_CHARACTER_4.3.2B188, OK
Wed Jan 15 10:36:21 MYT 2014, StartInstall, GAME_TILES_7.3.2B467, OK
Wed Jan 15 10:36:21 MYT 2014, StartInstall, GAME_TILES_7.3.2B467, OK
Wed Jan 15 10:37:33 MYT 2014, EndInstall, GAME_TILES_7.3.2B467, DONE
Wed Jan 15 10:37:35 MYT 2014, StartInstall, MOVIE_CUTSCENE_4.3.2B246, OK
Wed Jan 15 10:37:35 MYT 2014, StartInstall, MOVIE_CUTSCENE_4.3.2B246, OK
Wed Jan 15 10:38:21 MYT 2014, EndInstall, MOVIE_CUTSCENE_4.3.2B246, DONE
Wed Jan 15 10:38:59 MYT 2014, EndInstall, MOB_CHARACTER_4.3.2B188, DONE
Wed Jan 15 10:39:00 MYT 2014, StartPatch, GAME_TILES_7.3.2.ServicePackB1, OK
Wed Jan 15 10:39:00 MYT 2014, StartPatch, GAME_TILES_7.3.2.ServicePackB1, OK
Wed Jan 15 10:39:07 MYT 2014, EndPatch, GAME_TILES_7.3.2.ServicePackB1, DONE
Wed Jan 15 10:39:08 MYT 2014, StartPatch, GAME_TILES_7_3_2_3B1_Server, OK
Wed Jan 15 10:39:08 MYT 2014, StartPatch, GAME_TILES_7_3_2_3B1_Server, OK
Wed Jan 15 10:39:16 MYT 2014, EndPatch, GAME_TILES_7_3_2_3B1_Server, DONE
Wed Jan 15 10:39:16 MYT 2014, StartPatch, GAME_TILES_7_3_2_3B1_Client, OK
Wed Jan 15 10:39:16 MYT 2014, StartPatch, GAME_TILES_7_3_2_3B1_Client, OK
Wed Jan 15 10:39:23 MYT 2014, EndPatch, GAME_TILES_7_3_2_3B1_Client, DONE
Wed Jan 15 10:39:24 MYT 2014, StartPatch, GAME_TILES_7.3.2.1B1, OK
Wed Jan 15 10:39:24 MYT 2014, StartPatch, GAME_TILES_7.3.2.1B1, OK
Wed Jan 15 10:39:34 MYT 2014, EndPatch, GAME_TILES_7.3.2.1B1, DONE
Wed Jan 15 10:39:35 MYT 2014, StartPatch, GAME_TILES_7.3.2.1B1_simulator, OK
Wed Jan 15 10:39:35 MYT 2014, StartPatch, GAME_TILES_7.3.2.1B1_simulator, OK
Wed Jan 15 10:39:45 MYT 2014, EndPatch, GAME_TILES_7.3.2.1B1_simulator, DONE
Wed Jan 15 10:39:46 MYT 2014, StartPatch, MOVIE_CUTSCENE_4.3.2.1B1, OK
Wed Jan 15 10:39:46 MYT 2014, StartPatch, MOVIE_CUTSCENE_4.3.2.1B1, OK
Wed Jan 15 10:39:53 MYT 2014, EndPatch, MOVIE_CUTSCENE_4.3.2.1B1, DONE
Wed Jan 15 10:39:54 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.1B1, OK
Wed Jan 15 10:39:54 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.1B1, OK
Wed Jan 15 10:40:03 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.1B1, DONE
Wed Jan 15 10:40:04 MYT 2014, StartPatch, GAME_TILES_7.3.2.2B1_Server, OK
Wed Jan 15 10:40:04 MYT 2014, StartPatch, GAME_TILES_7.3.2.2B1_Server, OK
Wed Jan 15 10:40:12 MYT 2014, EndPatch, GAME_TILES_7.3.2.2B1_Server, DONE
Wed Jan 15 10:40:13 MYT 2014, StartPatch, GAME_TILES_7.3.2.2B1_Client, OK
Wed Jan 15 10:40:13 MYT 2014, StartPatch, GAME_TILES_7.3.2.2B1_Client, OK
Wed Jan 15 10:40:21 MYT 2014, EndPatch, GAME_TILES_7.3.2.2B1_Client, DONE
Wed Jan 15 10:40:22 MYT 2014, StartPatch, GAME_TILES_7.3.2.4B2, OK
Wed Jan 15 10:40:22 MYT 2014, StartPatch, GAME_TILES_7.3.2.4B2, OK
Wed Jan 15 10:40:32 MYT 2014, EndPatch, GAME_TILES_7.3.2.4B2, DONE
Wed Jan 15 10:40:33 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.2B1, OK
Wed Jan 15 10:40:33 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.2B1, OK
Wed Jan 15 10:40:42 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.2B1, DONE
Wed Jan 15 10:40:43 MYT 2014, StartPatch, MOVIE_CUTSCENE_4.3.2.2B3, OK
Wed Jan 15 10:40:43 MYT 2014, StartPatch, MOVIE_CUTSCENE_4.3.2.2B3, OK
Wed Jan 15 10:40:50 MYT 2014, EndPatch, MOVIE_CUTSCENE_4.3.2.2B3, DONE
Wed Jan 15 10:40:51 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.12B2, OK
Wed Jan 15 10:40:51 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.12B2, OK
Wed Jan 15 10:40:51 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.12B2, DONE
Wed Jan 15 10:40:52 MYT 2014, StartPatch, GAME_TILES_7.3.2.23B2, OK
Wed Jan 15 10:40:52 MYT 2014, StartPatch, GAME_TILES_7.3.2.23B2, OK
Wed Jan 15 10:40:52 MYT 2014, EndPatch, GAME_TILES_7.3.2.23B2, DONE
Wed Jan 15 10:40:53 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.13B1, OK
Wed Jan 15 10:40:53 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.13B1, OK
Wed Jan 15 10:41:07 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.13B1, DONE
Wed Jan 15 10:41:07 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.17B3, OK
Wed Jan 15 10:41:07 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.17B3, OK
Wed Jan 15 10:41:08 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.17B3, DONE
Wed Jan 15 10:41:09 MYT 2014, StartPatch, GAME_TILES_7.3.2.30B4, OK
Wed Jan 15 10:41:09 MYT 2014, StartPatch, GAME_TILES_7.3.2.30B4, OK
Wed Jan 15 10:41:09 MYT 2014, EndPatch, GAME_TILES_7.3.2.30B4, DONE
Wed Jan 15 10:41:10 MYT 2014, StartPatch, GAME_TILES_7.3.2.41B1, OK
Wed Jan 15 10:41:10 MYT 2014, StartPatch, GAME_TILES_7.3.2.41B1, OK
Wed Jan 15 10:41:10 MYT 2014, EndPatch, GAME_TILES_7.3.2.41B1, DONE
Wed Jan 15 10:41:11 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.13B2, OK
Wed Jan 15 10:41:11 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.13B2, OK
Wed Jan 15 10:41:26 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.13B2, DONE
Wed Jan 15 10:41:27 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.16B1, OK
Wed Jan 15 10:41:27 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.16B1, OK
Wed Jan 15 10:41:29 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.16B1, DONE
Wed Jan 15 10:41:30 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.22B2, OK
Wed Jan 15 10:41:30 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.22B2, OK
Wed Jan 15 10:41:30 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.22B2, DONE
Wed Jan 15 10:41:31 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.24B2, OK
Wed Jan 15 10:41:31 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.24B2, OK
Wed Jan 15 10:41:31 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.24B2, DONE
Wed Jan 15 10:41:32 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.25B2, OK
Wed Jan 15 10:41:32 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.25B2, OK
Wed Jan 15 10:41:32 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.25B2, DONE
Wed Jan 15 10:41:33 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.28B2, OK
Wed Jan 15 10:41:33 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.28B2, OK
Wed Jan 15 10:41:33 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.28B2, DONE
Wed Jan 15 10:41:34 MYT 2014, StartPatch, GAME_TILES_7.3.2.40B1, OK
Wed Jan 15 10:41:34 MYT 2014, StartPatch, GAME_TILES_7.3.2.40B1, OK
Wed Jan 15 10:41:34 MYT 2014, EndPatch, GAME_TILES_7.3.2.40B1, DONE
Wed Jan 15 10:41:35 MYT 2014, StartPatch, GAME_TILES_7.3.2.43B2, OK
Wed Jan 15 10:41:35 MYT 2014, StartPatch, GAME_TILES_7.3.2.43B2, OK
Wed Jan 15 10:41:36 MYT 2014, EndPatch, GAME_TILES_7.3.2.43B2, DONE
Wed Jan 15 10:41:36 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.32B5, OK
Wed Jan 15 10:41:36 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.32B5, OK
Wed Jan 15 10:41:37 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.32B5, DONE
Wed Jan 15 10:41:37 MYT 2014, StartPatch, GAME_TILES_7.3.2.33B3, OK
Wed Jan 15 10:41:37 MYT 2014, StartPatch, GAME_TILES_7.3.2.33B3, OK
Wed Jan 15 10:41:38 MYT 2014, EndPatch, GAME_TILES_7.3.2.33B3, DONE
Wed Jan 15 10:41:39 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.42B4, OK
Wed Jan 15 10:41:39 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.42B4, OK
Wed Jan 15 10:41:39 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.42B4, DONE
Wed Jan 15 10:41:40 MYT 2014, StartPatch, GAME_TILES_7.3.2.62B1_Server, OK
Wed Jan 15 10:41:40 MYT 2014, StartPatch, GAME_TILES_7.3.2.62B1_Server, OK
Wed Jan 15 10:41:51 MYT 2014, EndPatch, GAME_TILES_7.3.2.62B1_Server, DONE
Wed Jan 15 10:41:53 MYT 2014, StartPatch, GAME_TILES_7.3.2.62B1_Client, OK
Wed Jan 15 10:41:53 MYT 2014, StartPatch, GAME_TILES_7.3.2.62B1_Client, OK
Wed Jan 15 10:42:02 MYT 2014, EndPatch, GAME_TILES_7.3.2.62B1_Client, DONE
Wed Jan 15 10:42:03 MYT 2014, StartPatch, MOVIE_CUTSCENE_4.3.2.15B1, OK
Wed Jan 15 10:42:03 MYT 2014, StartPatch, MOVIE_CUTSCENE_4.3.2.15B1, OK
Wed Jan 15 10:42:12 MYT 2014, EndPatch, MOVIE_CUTSCENE_4.3.2.15B1, DONE
Wed Jan 15 10:42:12 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.32B7, OK
Wed Jan 15 10:42:12 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.32B7, OK
Wed Jan 15 10:42:23 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.32B7, DONE
Wed Jan 15 10:42:24 MYT 2014, StartPatch, GAME_TILES_7.3.2.66B4, OK
Wed Jan 15 10:42:24 MYT 2014, StartPatch, GAME_TILES_7.3.2.66B4, OK
Wed Jan 15 10:42:25 MYT 2014, EndPatch, GAME_TILES_7.3.2.66B4, DONE
Wed Jan 15 10:42:26 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.55B1, OK
Wed Jan 15 10:42:26 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.55B1, OK
Wed Jan 15 10:42:35 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.55B1, DONE
Wed Jan 15 10:42:36 MYT 2014, StartPatch, GAME_TILES_7.3.2.68B1, OK
Wed Jan 15 10:42:36 MYT 2014, StartPatch, GAME_TILES_7.3.2.68B1, OK
Wed Jan 15 10:42:37 MYT 2014, EndPatch, GAME_TILES_7.3.2.68B1, DONE
Wed Jan 15 10:42:38 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.57B1, OK
Wed Jan 15 10:42:38 MYT 2014, StartPatch, MOB_CHARACTER_4.3.2.57B1, OK
Wed Jan 15 10:42:45 MYT 2014, EndPatch, MOB_CHARACTER_4.3.2.57B1, DONE
答案 0 :(得分:2)
是的,这是一种方式:
awk '$9 == "DONE" { key = $8; add = ""; sub ("_CLIENT,$",",",key); if (key != $8) { add = "_CLIENT," } else { sub ("_SERVER,$",",",key); if (key != $8) { add = "_SERVER," } } sub ("_[^_]*$", "", key); key = key""add; assoc[key] = $0; } END { for (var in assoc) { print assoc[var] } }' inputFile.txt
现在这很难看,所以我会将其分解并添加注释,以便您可以理解它是如何工作的:
$9 == "DONE" { // Only want "DONE" lines.
key = $8; // Get the key.
add = "" // Save client/server/none and remove
sub ("_CLIENT,$", ",", key);
if (key != $8) {
add = "_CLIENT,"
} else {
sub ("_SERVER,$", ",", key);
if (key != $8) {
add = "_SERVER,"
}
}
sub ("_[^_]*$", "", key); // Remove version.
key = key""add; // Put client/server/none back.
assoc[key] = $0; // Store line.
}
END {
for (var in assoc) { // Print out every keyed line.
print assoc[var]
}
}
老实说,一旦awk
脚本变得复杂,最好将它们放入script.awk
文件并使用awk -f
运行 - 在命令上执行这些长脚本线有点痛苦。
这里的技巧是使用密钥计算和关联数组。从字段8中删除版本意味着它将成为“功能”的有效密钥,例如GAME_SERVER
或TILES_
。
完成此任务的方式如下:
sub
命令将第一个参数(_CLIENT,$
表示行尾_CLIENT,
)替换为给定变量中的第二个参数(只是逗号)({{1} })。key
现在与key
不同,我们保存该事实,不用担心检查服务器。$8
只是删除了(包括)最终sub
字符后的所有内容。_
,但删除了版本。设置该键的关联数组值将创建一个新条目或覆盖当前条目,这意味着具有相同键的文件中的后一行将覆盖先前的行。
然后,最后,您只需遍历关联数组,输出值(输入文件中的行)。在我的系统上,您会看到:
$8
根据需要。