Regex 使用批处理文件为关键字的每个实例创建换行符
我在一个系统上发现了一个0天的Java漏洞,这让我想到了在每个用户配置文件中查找可能指向活动0天威胁的可疑URL的Java.idx文件 我从这个开始:Regex 使用批处理文件为关键字的每个实例创建换行符,regex,windows,command-line,batch-file,Regex,Windows,Command Line,Batch File,我在一个系统上发现了一个0天的Java漏洞,这让我想到了在每个用户配置文件中查找可能指向活动0天威胁的可疑URL的Java.idx文件 我从这个开始: 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的文件,
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://”实例创建一个换行符。有人知道我如何在批处理文件中使用命令行来实现这一点吗?我知道“只需使用其他工具!”答案可能会很烦人。然而,Windows批处理文件/命令行实用程序非常糟糕,如果你不得不经常做这类事情,我认为值得研究其他选项。除非你喜欢搞清楚,也就是说
一些选择:
- (与findstr相比,这是一个更好的等价物)
- (适用于Windows的完整的类似Linux的shell环境;包括grep)
http://
之前添加一个换行符:
perl -pi.bak -e "s|(http://)|\n$1|g" C:\temp\javaurls.txt
然而,如果我从头开始,我会用Perl来完成整个过程,而不是使用批处理文件。我大体上同意其他人表达的观点——批处理在处理文本方面很糟糕。使用另一种语言或工具是完成任务的好主意 但它可以通过批处理完成:-) 简单地在每个
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
我会安装Perl或Grep,然后使用它。除非你喜欢痛苦。@AndyG如果你不想安装Perl,你可以用它来代替。对于基于regexp的搜索/替换操作来说,它是一个很棒的命令行工具。您可以下载Windows的免费版本,只需使用dan1111的regexp字符串。结果是一样的,谢谢大家。为了及时性,我决定运行: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)perl-pi.bak-e“S |(http:/)\n$1 | g”C:\temp\javaurls.txt我想我需要学习如何使用perl。dbenham:cmd行在很大程度上起作用,但perl的输出要整洁得多。