中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-12 14:16
47,811 主题排行 / 349,897 发帖 / 今日 0 篇 / 48,256 会员排行
DOS批处理 & 脚本技术(批处理室) » 求解难题-读取日志文件
可打印版本  1,449 / 9
第1楼 pitt0303 发表于 2010-04-07 04:24
新手上路 发帖 6 积分 6
求解难题-读取日志文件
我需要读取这个日志文件, 这个文件的格式是:

<startedTimestamp>2010-03-02T22:39:55+01:00</startedTimestamp>
<event name="InstallationStarted">2010-03-02T22:39:55+01:00</event>
<event name="IdleCheckPassed">2010-03-02T22:40:05+01:00</event>
<event name="MonitorDeploymentStarted">2010-03-02T22:40:05+01:00</event>
<event name="MonitorDeploymentCompleted">2010-03-02T22:41:20+01:00</event>
<event name="CustomizationPointBeforeTheWholeUpgradeStarted">2010-03-02T22:41:20+01:00</event>
<event name="CustomizationPointBeforeTheWholeUpgradeCompleted">2010-03-02T22:41:20+01:00</event>
<event name="SanityChecksStarted">2010-03-02T22:41:20+01:00</event>
<event name="InstallationFailed">2010-03-02T22:41:27+01:00</event>
<errorMessage>Please attend to pending fuel price changes before attempting an upgrade.</errorMessage>
<stackTrace>Radiant.RPOS.InstallRPOS.Exceptions.InstallationAttemptFailedException: Please attend to pending fuel price changes before attempting an upgrade.
at Radiant.RPOS.InstallRPOS.Engine.ManifestInterpreter.ProcessBuiltinInstallationManifest()
at Radiant.RPOS.InstallRPOS.Engine.InstallRPOS.Main()</stackTrace>
<stoppedTimestamp>2010-03-02T22:41:27+01:00</stoppedTimestamp>

然后将这个日志读取并采用一个格式输出到文本里面,例如:

Example – parsing an <event> node
<event name="InstallationStarted">2010-03-02T22:39:55+01:00</event>
Parses to:
EventType = Event
EventDescription = InstallationStarted
EventTime = 2010-03-02T22:39:55+01:00

Example – parsing an <errorMessage> node
<errorMessage>Please attend to pending fuel price changes before attempting an upgrade.</errorMessage>
Parses to:
EventType = Error
EventDescription = Please attend to pending fuel price changes before attempting an upgrade.

Example – parsing a <startedTimestamp> node
<startedTimestamp>2010-03-02T22:39:55+01:00</startedTimestamp>
Parses to:
EventType = Started
EventTime = 2010-03-02T22:39:55+01:00

我原本是想逐行读取的,但现在按照这个要求是不行了. 希望标准的输入格式:

<Event><InstallationStarted><2010-03-02T22:39:55+01:00>
<Error><Please attend to pending fuel price changes before attempting an upgrade.>

这里不需要考虑<stackTrace>的信息,可以直接跳过不处理.

由于小弟几乎没接触过CMD的程序,而且这次任务又很急,所以想请大家帮忙.

谢谢
第2楼 Hanyeguxing 发表于 2010-04-07 11:44
银牌会员 发帖 897 积分 1,039 来自 在地狱中仰望天堂
具体说明描述你想做什么
第3楼 pitt0303 发表于 2010-04-07 13:06
新手上路 发帖 6 积分 6
读取以下的日志文件,然后按规定的格式输出到新的文本。

这个是原始的日志文件样式:

<startedTimestamp>2010-03-02T22:39:55+01:00</startedTimestamp>
<event name="InstallationFailed">2010-03-02T22:41:27+01:00</event>
<errorMessage>Please attend to pending fuel price changes before attempting an upgrade.</errorMessage>
<stackTrace>Radiant.RPOS.InstallRPOS.Exceptions.InstallationAttemptFailedException: Please attend to pending fuel price changes before attempting an upgrade.
at Radiant.RPOS.InstallRPOS.Engine.ManifestInterpreter.ProcessBuiltinInstallationManifest()
at Radiant.RPOS.InstallRPOS.Engine.InstallRPOS.Main()</stackTrace>
<stoppedTimestamp>2010-03-02T22:41:27+01:00</stoppedTimestamp>

要求输入的文本格式要求有4个变量,一个是事件类型,一个是事件内容,还有时间,最后是bat读取这个日志最新信息的时间。

Example – parsing an <event> node
<event name="InstallationStarted">2010-03-02T22:39:55+01:00</event>
Parses to:
EventType = Event
EventDescription = InstallationStarted
EventTime = 2010-03-02T22:39:55+01:00

Example – parsing an <errorMessage> node
<errorMessage>Please attend to pending fuel price changes before attempting an upgrade.</errorMessage>
Parses to:
EventType = Error
EventDescription = Please attend to pending fuel price changes before attempting an upgrade.

Example – parsing a <startedTimestamp> node
<startedTimestamp>2010-03-02T22:39:55+01:00</startedTimestamp>
Parses to:
EventType = Started
EventTime = 2010-03-02T22:39:55+01:00


如果事件是stackTrace则不读取。

不知道说清楚了没有。

[ Last edited by pitt0303 on 2010-4-7 at 13:12 ]
第4楼 exzzz 发表于 2010-04-07 23:27
初级用户 发帖 167 积分 194
我觉得过滤可以用 findstr
/V是只显示不包含关键词的行,将其转向输出到某个文件就过滤掉那些信息了。

用for命令,用 / 做为分隔符,只读取前面的变量转向到一个文件,就去掉结尾句了
然后用<跟>作为分隔符,读取变量并一行行的转向到某个文件就OK了啊
第5楼 pitt0303 发表于 2010-04-08 00:53
新手上路 发帖 6 积分 6
那是不是会比较麻烦呢,因为日志文件里面的内容很多啊

[ Last edited by pitt0303 on 2010-4-8 at 01:04 ]
第6楼 exzzz 发表于 2010-04-08 10:31
初级用户 发帖 167 积分 194
我觉得不麻烦吧,也就3小段脚本就可以解决了,如果关闭显示,很快就处理完了。
第7楼 pitt0303 发表于 2010-04-08 10:52
新手上路 发帖 6 积分 6
能不能麻烦给我一个例子了,我对批处理不是很了解。先谢谢了。
第8楼 exzzz 发表于 2010-04-08 12:59
初级用户 发帖 167 积分 194
第9楼 Hanyeguxing 发表于 2010-04-08 14:56
银牌会员 发帖 897 积分 1,039 来自 在地狱中仰望天堂
源文件:a.log,输出文件:b.log,按照1楼的样式猜测你的意思。。。。
第10楼 pitt0303 发表于 2010-04-09 23:25
新手上路 发帖 6 积分 6
谢谢大家的帮忙,问题已经解决,非常谢谢你们.
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023