美好的一天。
我有一堆FTP日志。现在,我想找出哪些用户在给定的时间范围内使用FTP。现在,我将该时间范围的一堆日志文件复制到一个文件夹中,因此我想让一个批处理脚本进行以下操作:在文件夹中使用所有日志文件,然后搜索以下学期,这些术语是独特的日志文件中列出的所有用户:
USER stblma 331
其中 stblma 是用户。因此,"用户"one_answers" 331"将是日志上所有登录的标准配置
因此,现在脚本必须将用户名复制到同一文件夹中的新临时文件,例如。users.txt
我该如何解决?
我将以示例为例。
logs access-log.log
USER jmacro
331 Password required for jmacro
PASS
230-Checking disk usage, please wait.
230-
230- Your disk quota is: 5.00 megabytes.
230- Your disk usage is:
230- Home/WWW: 1.23 megabytes
230- FTP: 0.00 megabytes
230- Total: 1.23 megabytes (25% of quota)
230-
230 User jmacro@macromedia.com logged in.
SYST
215 UNIX Type: L8 Version: BSD-198911
PWD
257 "/jmacro" is current directory.
USER nano
331 Password required for jmacro
PASS
230-Checking disk usage, please wait.
230-
230- Your disk quota is: 5.00 megabytes.
230- Your disk usage is:
230- Home/WWW: 1.23 megabytes
230- FTP: 0.00 megabytes
230- Total: 1.23 megabytes (25% of quota)
230-
230 User nano@macromedia.com logged in.
SYST
215 UNIX Type: L8 Version: BSD-198911
PWD
257 "/nano" is current directory.
这应该过滤用户名:
编辑
现在,脚本删除了重复
filter.bat
@echo off
:: Filter user names
for %%f in (logs*.log) do (
type "%%f" | find "USER" >> _temp.txt
)
if exist _temp.txt goto getUser
echo/No users or logs found&pause>nul&exit
:getUser
if not exist users.txt cd 1>nul 2>users.txt
for /f "tokens=2" %%u in ('type _temp.txt') do (
call:makefile %%u
)
del _temp.txt&exit
:makefile
type users.txt | find /i "%~1" >nul && goto already
echo/%~1 >> users.txt
:already
exit/b
尝试以下:
for /f "tokens=2" %%a in ('findstr /r "USER.*331" *.log') do echo %%a>>users.txt
这是一个版本,它将删除重复的用户并留下每个用户的一个实例:
@echo off
(for /f "tokens=1*" %%a in ('findstr "USER" *.log') do echo %%b) >temp.tmp
sort <temp.tmp|uniq >users.txt
del temp.tmp
将其保存为uniq.bat
,然后将其放入与批处理文件或路径上的目录中的同一文件夹中:
@if (@CodeSection == @Batch) @then
@CScript //nologo //E:JScript "%~F0" & goto :EOF & Rem aacini 2013
@end
var line, prevLine = "";
while ( ! WScript.Stdin.AtEndOfStream ) {
line = WScript.Stdin.ReadLine();
if ( line != prevLine ) {
WScript.Stdout.WriteLine(line);
prevLine = line;
}
}
编辑 - 将重定向添加到最终文件
编辑2-最后,重复删除了。没有足够的日志文件来捕获它。
@echo off
setlocal enableextensions disabledelayedexpansion
set "user="
(for /f "tokens=2" %%a in (
'find "USER" "*.log" ^| findstr /r "^USER" ^| sort'
) do (
setlocal enabledelayedexpansion
if not "%%a"=="!user!" echo(%%a
endlocal
set "user=%%a"
)) > "users.txt"
endlocal