读取日志文件并查找值字符串 [英] Log file reading and finding a value string

查看:100
本文介绍了读取日志文件并查找值字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志文件,其中包含以下几行.

I have got a log file which contains the below lines in it.

总测试:1.通过:0.失败:1.跳过:0.
测试执行时间:3.5167分钟
总计测试:1.通过:0.失败:1.跳过:0.

Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.
Test execution time: 3.5167 Minutes
Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.

@Echo off
Set "log_file=Automation_Log_20191125_1853_06.log"
Call :Main
GoTo :EOF

:Main
for /f "delims=" %%i in ('findstr /i "Failed: 0." %log_file%') do (
  set line=%%i
  HERE I NEED HELP
)
Exit /B

当存在类似于 Failed:1 的值时,将以日志文件作为附件发送一封电子邮件.如果失败:0.不执行任何操作.

When there is a value which is like Failed: 1 an email to be initiated with the log file as an attachment. If Failed: 0. do nothing.

@Mofi我添加了这样的代码,应该是这样吗?

@Mofi I added the code like this, is this how it should be?

@Echo off
Set "log_file=Automation_Log_20191125_1853_06.log"
Call :Main
GoTo :EOF

:Main
findstr /R /C:"^Total tests: .*Failed: [123456789]" %log_file% >nul
if not errorlevel 1 
echo Send the log file Automation_Log_20191125_1853_06.log with an email.
Exit /B

推荐答案

看起来只有在日志文件包含字符串Failed: XX是大于0的数字​​时,它才有意义.因此,完全不需要 FOR 循环.在整个文件中搜索这样的字符串就足够了,也许在正确的上下文中,例如在行的开头必须有Total tests:以避免误报,然后仅评估下一个是否在日志文件中找到该字符串即可.

It looks like it only matters if the log file contains the string Failed: X with X being a number greater 0. For that reason the FOR loop is not necessary at all. It is enough to search entire file for such a string, perhaps in right context like there must be Total tests: at beginning of the line to avoid false positives, and just evaluate next if the string is found in the log file or not.

如所讨论的代码中那样运行以下命令行会发生什么?

What happens on running the following command line as in code in question?

findstr /i "Failed: 0." Automation_Log_20191125_1853_06.log

仅指定选项/i进行不区分大小写的搜索.

There is only specified the option /i to do a case-insensitive search.

既没有使用选项/L显式请求文字搜索,也没有使用选项/R显式请求正则表达式搜索.仅使用双引号将其指定为搜索字符串.在这种情况下 FINDSTR 分析搜索字符串并确定是使用文字搜索还是正则表达式搜索.

There is neither used option /L to explicitly request a literal search nor the option /R to explicitly request a regular expression search. The search string is specified with just putting it in double quotes. In this case FINDSTR analyzes the search string and determines itself if using a literal or a regular expression search.

将包含空格字符的双引号中指定的搜索字符串解释为正则表达式字符串,并将空格解释为 OR 表达式.

A search string specified in double quotes containing a space character is interpreted as regular expression string with interpreting the space as OR expression.

命令提示符窗口中执行findstr /?输出命令 FINSTR 描述正则表达式字符及其含义.

The execution of findstr /? in a command prompt window outputs the help of command FINSTR describing the regular expression characters and their meanings.

默认情况下,/C:"Failed: 0"的使用将在文字搜索中(/L在未明确指定时隐式使用),将空格字符解释为文字空间,将解释为 OR 表达式.这是"search string"/C:"search string"之间最重要的区别.即使在另外使用/R来运行正则表达式搜索时,/C:"search string"中的空格也始终被解释为文字字符,而搜索字符串在/C:之后用双引号引起来.参数"search string"中没有/C:的空格仅在使用附加选项/L时才解释为空格.在将"search string"/R或不与/L一起使用时,空格被解释为 OR 表达式.

The usage of /C:"Failed: 0" would result by default in a literal search (/L is used implicit on not being specified explicit) with interpreting the space character as literal space and not as OR expression. That is the most important difference between "search string" and /C:"search string". The space in /C:"search string" is always interpreted as literal character, even on using additionally /R to run a regular expression search with the search string in double quotes after /C:. The space in argument "search string" without /C: is interpreted as space only on using additionally option /L. The space is interpreted as OR expression on using "search string" with /R or without /L.

结论:

findstr "word1 word2 word3"
findstr /R "word1 word2 word3"

FINDSTR 正在使用正则表达式搜索包含word1 OR word2 OR word3的行.

FINDSTR is searching with a regular expression for lines containing word1 OR word2 OR word3.

findstr /C:"word 1" /C:"word 2" /C:"word 3"
findstr /L /C:"word 1" /C:"word 2" /C:"word 3"

FINDSTR 正在使用文字搜索来搜索包含word 1 OR word 2 OR word 3且空格被解释为的行空间.

FINDSTR is searching with a literal search for lines containing word 1 OR word 2 OR word 3 with space being interpreted as space.

建议始终使用/L/R FINDSTR 和命令行读者(如果将"search string"/C:"search string"解释为文字)100%清除搜索字符串或作为正则表达式搜索字符串.

It is advisable to always use /L or /R to make it 100% clear for FINDSTR and for readers of the command line if "search string" or /C:"search string" is interpreted as literal search string or as regular expression search string.

因此,此命令行导致搜索包含不区分大小写的字符串Failed: OR 字符串0的行.绝对不希望进行这种搜索,因为它总是会找到与编号无关的Failed:行.

So this command line results in searching for lines containing case-insensitive either the string Failed: OR the string 0. Such this search is definitely not wanted here as it will always finds the line with Failed: independent on the number.

在退出时返回调用过程的 FINDSTR 的值至少在找到符合搜索条件的一行上为0(搜索成功),或者为找不到符合搜索条件的行上的1 (搜索失败). FINDSTR 的退出代码是由cmd.exe调用findstr.exeERRORLEVEL分配的.

The value of FINDSTR returned to calling process on exit is 0 on at least one line found matching the search criteria (search successful) or 1 on no line found matching the search criteria (search failed). The exit code of FINDSTR is assigned by cmd.exe calling findstr.exe to ERRORLEVEL.

因此以下代码可用于搜索区分大小写Total tests:开头并且还包含Failed: 且下一个字符为更大的数字0的行,并评估退出找到符合搜索条件的行的成功代码.

So the following code could be used to search case-sensitive for a line starting with Total tests: and containing also Failed:  with next character being a digit greater 0 and evaluate the exit code for success on finding a line matching the search criteria.

%SystemRoot%\System32\findstr.exe /R /C:"^Total tests: .*Failed: [123456789]" Automation_Log_20191125_1853_06.log >nul
if not errorlevel 1 echo Send the log file Automation_Log_20191125_1853_06.log with an email.

默认情况下,用/C:指定的搜索字符串被解释为文字搜索字符串,但是/R会覆盖它,因此该搜索字符串被解释为正则表达式字符串,但是空格被解释为空格字符而不是 OR 表达式,因为省略/C:就是这种情况. ^表示行首,.*表示任何字符0次或多次,[123456789]表示方括号内的字符之一.

A search string specified with /C: is by default interpreted as literal search string, but /R overrides that and so the search string is interpreted as regular expression string, but with space being interpreted as space character and not as OR expression as it would be the case on omitting /C:. ^ means beginning of line and .* means any character 0 or more times and [123456789] means one of the characters inside the square brackets.

FINDSTR 在正匹配项上输出的行通过将其重定向到设备 NUL 而被取消,因为这些行实际上并不是必需的.

The lines output by FINDSTR on a positive match are suppressed by redirecting them to device NUL as those lines are not really needed.

仅当 FINDSTR 的值小于1(即值为0)退出时才有意义,因为这意味着日志文件包含报告至少一项失败操作的行.

It is only of interest if FINDSTR exited with a value less than 1 which means with value 0 because that means the log file contains a line reporting at least one failed operation.

另请参阅运行if /?时的帮助输出,并阅读:

See also the help output on running if /? and read:

  • Single line with multiple commands using Windows batch file
  • Microsoft article about Using command redirection operators

这篇关于读取日志文件并查找值字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆