使用特定范围的Findstr或其他方法

我只想在他的文本文件中find特定的行,所以我认为使用范围将是一个好主意。 但我似乎可以find任何在线教程。 请帮忙..

文本文件的一个例子

XXX scan report for 192.0.0.0 exampleexampleexampleexampleexample OS: windows 8 exampleexampleexampleexample exampleexampleexampleexample PORT STATE SERVICE VERSION 21/tcp close ftp 80/tcp open http Microsoft ISS exampleexampleexampleexample exampleexampleexampleexample XXX scan report for 192.0.0.1 exampleexampleexampleexampleexample exampleexampleexampleexample exampleexampleexampleexample PORT STATE SERVICE VERSION 21/tcp close ftp 80/tcp open http Microsoft ISS exampleexampleexampleexample exampleexampleexampleexample 

我想将IP地址,操作系统详细信息和端口状态放到电子表格中。

我的代码:

 @echo off set file=C:\Users\1.txt set match=report for set match1=OS findstr /c:"%match%" %file% findstr /c:"%match1%" %file% findstr /c:"tcp " /c:"udp " %file% for /f "tokens=1-5" %%a in ('findstr /c:"%match%" %file%') do ( echo "IP Address:","%%e" >> report1.csv for /f "tokens=1,2 delims=:*" %%a in ('findstr /c:"%match1%" %file%') do ( echo "Operating System: ","%%b" >> report1.csv echo "PORT","STATE","SERVICE","VERSION" >> report1.csv for /f "tokens=1-4 delims=: " %%a in ('findstr /c:"tcp " /c:"udp " %file%') do ( echo "%%a","%%b","%%c","%%d" >> report1.csv ) ) ) 

这段代码有一个很大的问题,它在for循环中。 代码将获得第一个IP然后将继续获取操作系统的细节,但没有任何IP有操作系统的细节,所以操作系统的细节将被放置在错误的IP地址。

代码的另一个问题是,它将列出一个IP地址下的所有操作系统和端口的详细信息。 而下一个IP地址也将是一样的,它将具有所有的操作系​​统和端口的细节。

请帮我解决这个问题。 还是有其他的方法,如电话?

 @ECHO OFF SETLOCAL SET "sourcedir=U:\sourcedir" SET "destdir=U:\destdir" SET "filename1=%sourcedir%\q43335765.txt" SET "outfile=%destdir%\outfile.txt" >"%outfile%" ECHO IP,OS,PORT,STATUS,SERVICE,VERSION FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO ( CALL :process %%a ) GOTO :EOF :process ECHO %*|FIND "scan report for " >NUL IF NOT ERRORLEVEL 1 GOTO newip IF "%~1"=="OS:" SET "$os=%*"&GOTO :eof ECHO %~1|FIND "/">NUL IF ERRORLEVEL 1 GOTO :EOF FOR /f "tokens=1,2,3*" %%p IN ( "%*") DO >>"%outfile%" ECHO %$ip%,%$os:*: =%,%%p,%%q,%%r,%%s GOTO :eof :newip IF "%~2" neq "" shift&GOTO newip SET "$ip=%~1" SET "$os=: " GOTO :eof 

您将需要更改sourcedirdestdir的设置以适合您的情况。

我使用了一个名为q43335765.txt的文件, q43335765.txt包含您的数据用于测试。

生成定义为%outfile%的文件

建立文件名后,将标题行放在输出文件中,并通过:process文件的每一行

:process ,检测指示ne数据项的键字符串。 如果找到,则转到:newip ,它只是简单地将数据行:newip ,直到只剩下最后一个条目,并将最后一个条目分配给$ip 。 设置$ip: 空格 +任何特殊的字符串,你想表示“找不到”(如未知

如果该行不包含keystring,请查看第一个标记是否为OS: :,如果是,则将$os设置$os整个原始行。

否则,在第一个标记中寻找/ 。 如果不存在,则放弃处理该行,否则只需简单地标记该行,选择第一个休息和输出,使用已保存的ip,已保存的os,除了第一个空格和四个数据行条目之前的部分,全部用逗号分隔。


修改为迎合| 在数据中 – 用/

 @ECHO OFF SETLOCAL SET "sourcedir=U:\sourcedir" SET "destdir=U:\destdir" SET "filename1=%sourcedir%\q43335765.txt" SET "outfile=%destdir%\outfile.txt" >"%outfile%" ECHO IP,OS,PORT,STATUS,SERVICE,VERSION FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO ( SET "line=%%a" CALL :preprocess ) GOTO :EOF :preprocess SET "line=%Line:|=/%" CALL :process %line% GOTO :eof :process ECHO %*|FIND "scan report for " >NUL IF NOT ERRORLEVEL 1 GOTO newip IF "%~1"=="OS:" SET "$os=%*"&GOTO :eof ECHO %~1|FIND "/">NUL IF ERRORLEVEL 1 GOTO :EOF FOR /f "tokens=1,2,3*" %%p IN ( "%*") DO >>"%outfile%" ECHO %$ip%,%$os:*: =%,%%p,%%q,%%r,%%s GOTO :eof :newip IF "%~2" neq "" shift&GOTO newip SET "$ip=%~1" SET "$os=: " GOTO :eof 

显示需要提供具有代表性的数据样本…

与像|一样具有特殊含义的字符 ,分配给一个变量并调用一个预处理器来转换所有的|/ (或任何其他需要的), 然后 call结果的过程。

公式:set“var =%somevar: string1 = string2 %”

将var赋值为var的所有出现的字符串1替换为字符串2。 set命令中的封闭引号确保行中的任何零散尾随空格不包含在赋值中。

你的逻辑中的缺陷是,每次执行findstr ,都会从文本文件的第1行开始。 如果您正在使用多行记录,则必须通过记录一次构建您的输出行。 我建议一个for /F循环,测试令牌比findstr更适合这个。 这里有一个建议:

 @echo off setlocal set "file="C:\Users\1.txt" >"report1.csv" ( echo "IP Address","Operating System","port 21","port 80" for /F "usebackq tokens=1-3,5" %%I in ("%file%") do ( rem // check if line begins a record if /i "%%~J"=="scan" if /i "%%~K"=="report" ( set "IP=%%~L" set "OS=" ) rem // check if line contains OS if /i "%%~I"=="OS:" set OS="%%~J %%~K" rem // check if line contains port 21 if /i "%%~I"=="21/tcp" set "state21=%%~J" rem // port 80 indicates end of needed info in record setlocal enabledelayedexpansion if /i "%%~I"=="80/tcp" echo(!IP!,!OS!,!state21!,%%~J endlocal ) )