繁体   English   中英

盘点数量

[英]Count number of |

我需要有关脚本的帮助,该脚本应该计算|的数量。 在特定字符串之前。

info.txt

text=jam|hello=123|result=ok|cow=cat|...

因此,在此示例中,如果您搜索result =,答案应该为2。

尝试这个:

@ECHO OFF &SETLOCAL
SET "string=text=jam|hello=123|result=ok|cow=cat|..."
SET "stop=result=ok"
SET "char=|"

SET /a count=-1
SET "org=%string%"
:loop
FOR /f "tokens=1*delims=%char%" %%a IN ("%string%") DO SET "this=%%a"&SET "that=%%b"
IF DEFINED that (SET "string=%that%") ELSE (SET "string=%this%")
SET /a count+=1
IF NOT DEFINED string (ECHO NOT found: "%stop%" &GOTO :EOF)
IF NOT "%this%"=="%stop%" GOTO :loop
ECHO Number of "%char%" IN "%org%" until "%stop%": %count%

这使用了一个名为repl.bat的帮助程序批处理文件:from- http ://www.dostips.com/forum/viewtopic.php?f=3&t=3855

如果您在searchstring.bat下面调用此代码,则可以像这样启动它

searchstring "result="

它期望每个文件只有一个匹配项,并且区分大小写。

@echo off
type "file.txt" | find "%~1" | repl "(.*).%~1.*" "$1" | repl "\x7c" "\r\n" x | find /c /v ""

下面的此批处理文件将在file.txt每一行中返回行号和行号本身的计数(当行号大于零时)。

@echo off
if "%~1"=="" ( echo add a search term&pause&goto :EOF)
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "file.txt" ') do (
for /f %%c in (' echo "%%b"^| find "%~1" ^| repl "(.*).%~1.*" "$1" ^| repl "\|" "\r\n" x ^| find /c /v "" ') do (
if %%c GTR 0 echo Line %%a: %%c
)
)
pause

如果需要,例如第三个字符串:

SET "text=jam|hello=123|result=ok|cow=cat|..."
FOR /F "TOKENS=3" %%t IN ("%text%") DO ECHO %%t

如果需要,例如第三个字符串及以下:

SET "text=jam|hello=123|result=ok|cow=cat|..."
FOR /F "TOKENS=2,*" %%t IN ("%text%") DO ECHO %%u

这是另一种方式(使用您的info.txt文件)。 不区分大小写。 处理文件中具有匹配字符串的多行。

@echo off
set "SpecificString=result=ok"
set /A cnt=0
for /F "tokens=*" %%A IN (info.txt) do (
   for /F "usebackq tokens=*" %%B IN (`echo."%%A" ^| find /I "%SpecificString%"`) do (
      call :Parse "%%~A"
      )
   )
pause
goto :eof

:Parse
for /F "usebackq tokens=1* delims=^|" %%B IN (`echo."%~1"`) do (
   if /I "%%~B"=="%SpecificString%" (
      echo.Cnt=%Cnt% in "%%A"
      echo.
      set /A Cnt=0
      goto :eof
      )
   set /A Cnt+=1
   call :Parse "%%~C
   )
goto :eof

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM