使用批处理文件为关键字的每个实例创建换行符

时间:2012-12-18 13:57:32

标签: regex windows command-line batch-file

我发现系统上曾经是一个0天的Java漏洞,它让我想到了查看每个用户配置文件中的Java .idx文件,看是否存在可能指向0天主动威胁的可疑URL。 / p>

我从这开始:

for /d %%A in ("C:\Documents and Settings\*") do (
findstr /S "http://" "%%A\Application Data\Sun\Java\Deployment\cache\6.0\*" >> C:\temp\javaurls.txt)

这会产生一个充满URL的文件,但它们与很多控制信息和其他无趣的东西混在一起。我想要做的是为每个找到的“http://”小写实例创建一个换行符。任何人都知道我如何使用批处理文件中的命令行来做到这一点?

2 个答案:

答案 0 :(得分:0)

我知道“只需使用其他工具!”答案可能很烦人。但是,Windows批处理文件/命令行实用程序是如此糟糕,我认为如果你必须做很多这样的事情,值得研究其他选项。除非您喜欢搞清楚undocumented, non-standard, broken regular expressionsneeding hacky solutions for even the most basic functionality,否则就是这样。

一些选项:

以下是一行Perl,您可以将其添加到批处理文件中,以便在每个http://之前添加换行符:

perl -pi.bak -e "s|(http://)|\n$1|g" C:\temp\javaurls.txt

但是,如果我从头开始,我会在Perl中完成所有操作,而不是使用批处理文件。

答案 1 :(得分:0)

我普遍同意其他人表达的观点 - 批处理在处理文本方面很糟糕。使用其他语言或工具对您的任务来说是一个好主意。

但可以批量处理: - )

只需在每个html://之前插入换行符就不那么难了

@echo off
setlocal disableDelayedExpansion
:: The first FOR loop loads a quoted linefeed into %%L
:: The blank line within the IN() clause is critical. Do not remove.
for %%L in (^"^

^") do (
  for /d %%A in ("C:\Documents and Settings\*") do (
    for /f "eol=: delims=" %%B in (
      'findstr /S "http://" "%%A\Application Data\Sun\Java\Deployment\cache\6.0\*"'
    ) do (
      set "line=%%B"
      setlocal enableDelayedExpansion
      echo(!line:http://=%%~Lhttp://!
      endlocal
    )
  )
) >c:\temp\javaurls.txt

但是只保留以http://开头的结果行,并且在地址变为真正的痛苦之前保留每个文件的名称。

@echo off
setlocal disableDelayedExpansion
:: The first FOR loop loads a quoted linefeed into %%L
:: The blank line within the IN() clause is critical. Do not remove.
for %%L in (^"^

^") do (
  for /d %%A in ("C:\Documents and Settings\*") do (
    for /f "tokens=1* delims=:" %%B in (
      'findstr /S "http://" "%%A\Application Data\Sun\Java\Deployment\cache\6.0\*"'
    ) do (
      set "line=%%C"
      setlocal enableDelayedExpansion
      set "line=!line:http://=%%~Lhttp://!"
      for /f "delims=" %%D in (
        '"cmd /v:on /c echo(^!line^!|findstr http://"'
      ) do (
        if "!!" equ "" endlocal
        echo %%B: %%D
      )
    )
  )
) >c:\temp\javaurls.txt
exit /b