使用批处理文件查找文件中的文本,然后清理它,保存到文件和变量

时间:2015-07-23 21:44:03

标签: batch-file cmd find findstr

好的,我搜索并搜索了几天,无法找到有效的方法。如果我错过了一些我真的很抱歉。

我的问题: 我有一个文本文件,其中包含网页的源代码。我的目标是搜索文本文件并查找。 “下面是我想要的线条”

<b>public</b>
</a>
</td>
<td></td>
<td class="b">
705330
</td>
<tr>
<tr>
<td>

(有更多的源代码与其他数字有相同的方式,但公众是唯一的。下面是唯一的(不是数字)现在,但我认为越匹配越好)

<td class="b">
705330
</td>

我正在尝试获取这些数字(因此必须删除除数字之外的所有内容),数字更改但其余部分不会。我想将数字保存到文件.txt(只是数字)(第一行和以上写入上一次保存)并分配给变量,以便它可以与之前的变量进行比较然后运行一些命令。

比如将新的(变量)与旧的.txt进行比较并做一些事情。

我把剩下的剩下来了,只是想不出来。香港专业教育学院尝试查找,findstr和每个论坛试图罚款对我有用的东西。 但是无法将搜索到的字符串变成一个变量,它只回显了大约30行或什么也没做。

我感谢任何帮助,提前致谢

1 个答案:

答案 0 :(得分:1)

好的 - 这很棘手......

批处理中,大多数应用程序都非常严格,因此处理这种高度灵活的文本(HTML)文件具有挑战性;我试过了......

以下批处理脚本假定:

  • 一个(文本)文件作为命令行参数给出;
  • &#34;&lt; b&gt; public&lt; / b&gt;&#34;字段是真正独特的(这不会被检查);
  • &#34;&lt; b&gt; public&lt; / b&gt;&#34;字段和&#34; class =&#34;令牌对区分大小写;
  • 总有一个&#34; class =&#34;令牌出现在&#34;&lt; b&gt; public&lt; / b&gt;&#34;之后场;
  • 兴趣的数量出现在&#34; class =&#34;之后的某个地方。令牌(可选地紧接在之后);
  • 感兴趣的数量以其独立的一行给出;

所以,让我们走吧(说明中的解释):

@echo off
setlocal EnableDelayedExpansion

rem set constant holding exact appearance of "public" field;
rem ^ escapes < and > which would otherwise constitute (unintended) redirections
set PUBLIC_TAG=^<b^>public^</b^>
rem set constant holding exact appearance of "class" token
set CLASS_PROP=class=
rem set constant to non-empty value if you want the target value to be right after the "class" token
set CLASS_GLUE=

rem initialise variable that holds line number of "public" field
set LinePublic=0
rem clear variable that is set as soon as "class" token is found
set FoundClass=
rem clear variable that will hold resulting (numeric) target value
set FieldValue=

rem check for command line argument being given
if "%1"=="" (echo No file given^^!& exit /B)

rem search for unique "public" field, return found line, prefix with line number;
rem the `2> nul` portion avoids displaying any `findstr` errors in case of input lines > 8192 chars.;
rem wrapped-around `for /F` retrieves line number only, stored in %LinePublic%
for /F "delims=:" %%L in ('type "%~1" ^| findstr /N /L "%PUBLIC_TAG%" 2^> nul') do set LinePublic=%%L
rem if no "public" field found, terminate batch script
if %LinePublic% equ 0 (echo File does not contain field "%PUBLIC_TAG%"^^!& exit /B)
rem starting at line number %LinePublic%, go through each line
for /F "usebackq skip=%LinePublic% delims=" %%F in ("%~1") do (
rem check if %FoundClass% has been set in (one of the) previous `for` iteration(s)
if defined FoundClass (
rem "class" token found previously, so check if target value has already been found
if not defined FieldValue (
rem no target value available yet, so check if current line contains decimal digits only
echo."%%F" | findstr /R "^\"[0-9][0-9]*\" \$" > nul
rem if ErrorLevel is 0 (below 1), current line constitutes one numeric value, so store it;
rem the `call` statement is necessary to avoid syntax errors due to < and > in line text %%F
if not ErrorLevel 1 ((call set FieldValue=%%F) & goto :FINE) else (
rem if you want the target value to be right after the "class" token, %CLASS_GLUE% must be set:
if defined CLASS_GLUE (echo No number follows "%CLASS_PROP%" token^^!& exit /B)
)))
rem search current line for "class" token; ErrorLevel is 0 if found
echo."%%F" | findstr /L "%CLASS_PROP%" > nul
rem if ErrorLevel is below 1, indicate by setting %FoundClass%, checked in next `for` iteration
if not ErrorLevel 1 set FoundClass=True
) & rem next %%F
:FINE
rem this compound statement makes %FieldValue% to survive `setlocal`/`endlocal` block
endlocal & set FieldValue=%FieldValue%
echo.%FieldValue%

这至少适用于您的文本文件示例......

提示:如果您希望在&#34; class =&#34;之后立即预期 。 token,将代码中的变量(常量)CLASS_GLUE设置为任何有效的非空值。

所以最后要完成将数字存储到文本文件中的任务,你需要输入:

above_batch_script_name.bat input_html_text_file.txt > output_text_file.txt

备注:由于批处理脚本在字符串操作和操作方面不是很强大,因此它们可能不是这种挑战的最佳选择。无论如何,我希望这有帮助...