Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将findstr用于多字符串和搜索_Regex_Batch File_Findstr - Fatal编程技术网

Regex 将findstr用于多字符串和搜索

Regex 将findstr用于多字符串和搜索,regex,batch-file,findstr,Regex,Batch File,Findstr,使用下面的方法,我能够计算出单个单词出现的总次数,并得到如下所示的结果 @echo off set "word=Windows" set file=log.txt set cnt=0 for /f ^"eol^=^ delims^=^" %%a in ('"findstr /i "/c:%word%" %file%"') do set "ln=%%a"&call :countWord echo Server_Type Total_Users >result.txt e

使用下面的方法,我能够计算出单个单词出现的总次数,并得到如下所示的结果

@echo off
set "word=Windows"
set file=log.txt
set cnt=0
for /f ^"eol^=^

delims^=^" %%a in ('"findstr /i "/c:%word%" %file%"') do set "ln=%%a"&call :countWord

echo Server_Type   Total_Users    >result.txt
echo %word%           %cnt%       >>result.txt
exit /b

:countWord
setlocal enableDelayedExpansion
:loop
if defined ln (
set "ln2=!ln:*%word%=!"
if "!ln2!" neq "!ln!" (
  set "ln=!ln2!"
  set /a "cnt+=1"
  goto :loop
)
  )
endlocal & set cnt=%cnt%
exit /b
result.txt

Server_Type  Total_Users
Windows          24
现在,我想添加6个新词,如Linux、MacOS、Andriod、Unix……等,以便在相同的日志文件中搜索,并以相同的格式获得结果


但是不知道如何使用FINDSTR实现这一点,并且考虑到FINDSTR有限的RegExp功能,这是可能的吗?有什么建议吗

我还没有尝试过这一点,但概念如下:

@echo off
set "word=Windows Linux MacOS ..."
set file=log.txt
set cnt=0
for %%i in (%word%) do (
for /f ^"eol^=^
...
<the rest of your code>
...
)
@echo关闭
设置“word=Windows Linux MacOS…”
set file=log.txt
设置cnt=0
对于%%i in(%word%)do(
适用于/楼^“下线^=^
...
...
)

我稍微修改了您的程序,以便将
cnt
变量转换为具有不同单词作为下标的变量,例如
cnt[Windows]=0
cnt[Linux]=0
,等等。因此
:countWords
子例程搜索每个匹配行中的所有单词。我还从
:countWords
子例程中删除了
setlocal
,以便以更简单的方式返回
cnt
数组的值

@echo off
setlocal EnableDelayedExpansion

set "words=Windows Linux MacOS Andriod Unix"
set file=log.txt
for %%a in (%words%) do set cnt[%%a]=0
for /f ^"eol^=^

delims^=^" %%a in ('"findstr /i "%words%" %file%"') do call :countWords "%%a"


(echo Server_Type   Total_Users    
for %%a in (%words%) do (
   echo %%a           !cnt[%%a]!       
)) > result.txt
exit /b

:countWords
set wordList=%words%
:nextWord
   for /F "tokens=1*" %%a in ("%wordList%") do (
      set word=%%a
      set wordList=%%b
   )
   set "ln=%~1"
   :loop
   if defined ln (
      set "ln2=!ln:*%word%=!"
      if "!ln2!" neq "!ln!" (
         set "ln=!ln2!"
         set /a "cnt[%word%]+=1"
         goto :loop
      )
   )
if defined wordList goto nextWord
exit /b

获取计数的一种更简单的方法:只需在搜索字符串的名称前加一个#,然后用
Set/a#%%a+=1
增加这个新变量的值(如果字符串真的相同),然后用
Set#
获得所有计数。非常简单有效。(对不起,我的英语糟透了)


嗨,阿奇尼,我已经测试了你的代码。如果你有“Unix\u foo”或“Windowstoto”“它将它们计算到。@sachadee..实际上我想计算单词Unix,Windows..不管它们是如何出现在日志中的..因为您也会同意,
Unix\u foo
Windowstoto
正在显示单词的出现情况
Unix
Windows
没有理由忽略它们..:)1。该程序仅当搜索词单独出现在一行中时才对其进行计数;2.它将使用字符串计算行数,而不是OP要求的单个字符串(这是因为程序非常复杂);3.它执行
findstr
命令的次数与目标字的次数相同(只执行一次更快,特别是当文件较大时)。PS-为什么在变量名中使用奇怪的字符,如
$
,或在参数中使用
%%*
?没有这些“辅助工具”,批处理文件就很难读取
8-
感谢您的回答@sachadee…但是对于一个日志文件,即使只有一个单词
windows
,它也不会给出正确的结果。要使所有发生,即使是
windowsSomething
您只需删除以下内容:
如果/i“%%*”==“%%a”
然后它将计算每次发生的次数。请检查日志文件中是否有
windows
在同一行中…忽略第二个windows单词。
@ECHO OFF

set "$file=log.txt"
set "$Lsearch=windows unix linux macOs Android"


for %%a in (%$LSearch%) do (set #%%a=0
                  for /f "delims=" %%* in ('type %$file% ^| findstr /i "%%a"') do if /i "%%*"=="%%a" set /a #%%a+=1)

for /f "tokens=1,2 delims==" %%a in ('set #') do echo Server : %%a Total User : %%b