批处理脚本读取每行文本文件并返回一个值?

时间:2022-07-12 02:29:11

I want to write a batch script that should read a text file line by line and search for a keyword.

我想编写一个批处理脚本,它应该逐行读取文本文件并搜索关键字。

If the keyword is found it should return value 0 else it should return 1. Like this it should keep on incrementing the value until the end of the file. The output should go to a file.

如果找到关键字,它应该返回值0,否则它应该返回1.像这样,它应该继续增加值直到文件的结尾。输出应该转到文件。

The sample log is as follows,

样本日志如下,

Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK

Basically I want to count the number of times the website failed. So, if the website is ok, then it should return value '0' if it is not then value '1'.

基本上我想计算网站失败的次数。所以,如果网站没问题,那么它应该返回值'0',如果它不是值'1'。

Last but not least I want the total number of times the website failed.

最后但并非最不重要的是,我想要网站失败的总次数。

3 个解决方案

#1


On Unix

fgrep 'keyword' log.txt | wc -l

#2


For Windows .bat:

对于Windows .bat:

FINDSTR /V /B /C:"HTTP/1.1 2" log.txt

will list every line that did not (/V) start with (/B) the literal (/C:) string "HTTP/1.1 2".

将列出没有(/ V)以(/ B)文字(/ C :)字符串“HTTP / 1.1 2”开头的每一行。

Since HTTP success codes are in the 200 block (eg, 203 for cache hit), this will only list non-success responses. FINDSTR does have some regex support, but it is pretty weak, (no grouping or alternation, especially), so if you want to filter out multiple response codes using FINDSTR you would have to chain them, eg to also filter out Auth failures:

由于HTTP成功代码在200块中(例如,203用于缓存命中),因此这将仅列出非成功响应。 FINDSTR确实有一些正则表达式支持,但它非常弱,(没有分组或替换,特别是),所以如果你想使用FINDSTR过滤掉多个响应代码,你必须链接它们,例如也过滤掉Auth失败:

FINDSTR /V /B /C:"HTTP/1.1 2" log.txt | FINDSTR /V /B /C:"HTTP/1.1 403"

#3


On DOS it would be the following line. Replace "FAIL STRING" with whatever a line will contain when the server has died.

在DOS上,它将是以下行。将“FAIL STRING”替换为服务器死亡时所包含的任何行。

FIND /C "FAIL STRING" log.txt

查找/ C“失败字符串”log.txt

This outputs something like the following when there are two lines containing "FAIL STRING"

当有两行包含“FAIL STRING”时,会输出如下内容

---------- LOG.TXT: 2

---------- LOG.TXT:2

You can also use %ERRORLEVEL% to determine if there were any failures. Like this:

您还可以使用%ERRORLEVEL%来确定是否存在任何故障。像这样:

FIND /C "FAIL STRING" log.txt
IF ERRORLEVEL 1 ECHO Everything is A-OK
IF NOT ERRORLEVEL 1 ECHO There were failures

FIND / C“FAIL STRING”log.txt如果ERRORLEVEL 1 ECHO一切都好,如果不是错误1 ECHO有失败

Good Luck,
Randy

好运,兰迪

#1


On Unix

fgrep 'keyword' log.txt | wc -l

#2


For Windows .bat:

对于Windows .bat:

FINDSTR /V /B /C:"HTTP/1.1 2" log.txt

will list every line that did not (/V) start with (/B) the literal (/C:) string "HTTP/1.1 2".

将列出没有(/ V)以(/ B)文字(/ C :)字符串“HTTP / 1.1 2”开头的每一行。

Since HTTP success codes are in the 200 block (eg, 203 for cache hit), this will only list non-success responses. FINDSTR does have some regex support, but it is pretty weak, (no grouping or alternation, especially), so if you want to filter out multiple response codes using FINDSTR you would have to chain them, eg to also filter out Auth failures:

由于HTTP成功代码在200块中(例如,203用于缓存命中),因此这将仅列出非成功响应。 FINDSTR确实有一些正则表达式支持,但它非常弱,(没有分组或替换,特别是),所以如果你想使用FINDSTR过滤掉多个响应代码,你必须链接它们,例如也过滤掉Auth失败:

FINDSTR /V /B /C:"HTTP/1.1 2" log.txt | FINDSTR /V /B /C:"HTTP/1.1 403"

#3


On DOS it would be the following line. Replace "FAIL STRING" with whatever a line will contain when the server has died.

在DOS上,它将是以下行。将“FAIL STRING”替换为服务器死亡时所包含的任何行。

FIND /C "FAIL STRING" log.txt

查找/ C“失败字符串”log.txt

This outputs something like the following when there are two lines containing "FAIL STRING"

当有两行包含“FAIL STRING”时,会输出如下内容

---------- LOG.TXT: 2

---------- LOG.TXT:2

You can also use %ERRORLEVEL% to determine if there were any failures. Like this:

您还可以使用%ERRORLEVEL%来确定是否存在任何故障。像这样:

FIND /C "FAIL STRING" log.txt
IF ERRORLEVEL 1 ECHO Everything is A-OK
IF NOT ERRORLEVEL 1 ECHO There were failures

FIND / C“FAIL STRING”log.txt如果ERRORLEVEL 1 ECHO一切都好,如果不是错误1 ECHO有失败

Good Luck,
Randy

好运,兰迪