Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file BatchScript循环日志文件并将特定标记写入新文件_Batch File - Fatal编程技术网

Batch file BatchScript循环日志文件并将特定标记写入新文件

Batch file BatchScript循环日志文件并将特定标记写入新文件,batch-file,Batch File,我想从一堆日志文件中提取信息,更具体地说,从日志文件每行的第2项和第3项中提取信息,然后写入新文件 问题还在于,这些条目中的许多都在每个日志文件中复制,所以我不想将重复的令牌/条目写入我要写入的新文件 基本上,它是一组包含登录信息的日志文件-IP和用户名位于日志文件中每行的第2和第3个位置。因此,我只想提取一次该信息,因为用户多次登录,并将每个唯一的IP和用户名写入一个新的日志文件 我有以下资料: for /f "tokens=2,3" in 'FTP_logs\*.log' >>

我想从一堆日志文件中提取信息,更具体地说,从日志文件每行的第2项和第3项中提取信息,然后写入新文件

问题还在于,这些条目中的许多都在每个日志文件中复制,所以我不想将重复的令牌/条目写入我要写入的新文件

基本上,它是一组包含登录信息的日志文件-IP和用户名位于日志文件中每行的第2和第3个位置。因此,我只想提取一次该信息,因为用户多次登录,并将每个唯一的IP和用户名写入一个新的日志文件

我有以下资料:

for /f "tokens=2,3" in 'FTP_logs\*.log' >> ftp_ips.txt
然而,这不起作用

谢谢,

for/f不会将FTP\u日志\识别为文件夹,因为没有表单可以识别。 要实现这一点,请将整个for/f包装在for循环中。

此外,批重定向是通过使用echo SOMETHING>>file.txt完成的

编辑:回复评论

我的程序有一个小错误,现在已经修复了;然而

当解决方案中存在任何文件/路径未找到错误时,由您自行修复。您知道计算机中存在的实际路径/文件名,因此只需对代码进行简单的调整,即可使其运行

您应该始终发布真实数据的示例,以便我们可以复制它并在解决方案中使用它。如果您不发布任何数据,我们将根据您提供的信息制定解决方案

根据您的描述,每个日志文件都有与以下类似的行:

token1 IPone USERone
token1 IPone USERtwo
token1 IPtwo USERone
token1 IPthree USERtwo
我准备了三个示例文件,并用它们修复了错误,运行了我的解决方案。以下是测试会话的输出:

C:\Users\Antonio\Documents\Test> dir
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Users\Antonio\Documents\Test

26/08/2015  10:49 a. m.    <DIR>          .
26/08/2015  10:49 a. m.    <DIR>          ..
26/08/2015  10:33 a. m.    <DIR>          Ftp_logs
26/08/2015  10:44 a. m.               414 test.bat
               1 archivos            414 bytes
               3 dirs  421,700,231,168 bytes libres

C:\Users\Antonio\Documents\Test> dir Ftp_logs
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Users\Antonio\Documents\Test\Ftp_logs

26/08/2015  10:33 a. m.    <DIR>          .
26/08/2015  10:33 a. m.    <DIR>          ..
26/08/2015  10:32 a. m.               204 One.log
26/08/2015  10:34 a. m.               216 Three.log
26/08/2015  10:33 a. m.               210 Two.log
               3 archivos            630 bytes
               2 dirs  421,700,231,168 bytes libres

C:\Users\Antonio\Documents\Test> type Ftp_logs\One.log
token1 IPone USERone
token1 IPone USERtwo
token1 IPone USERthree
token1 IPtwo USERone
token1 IPtwo USERtwo
token1 IPtwo USERthree
token1 IPone USERone
token1 IPone USERtwo
token1 IPone USERthree

C:\Users\Antonio\Documents\Test> type Ftp_logs\Two.log
token1 IPtwo USERone
token1 IPtwo USERtwo
token1 IPtwo USERthree
token1 IPthree USERone
token1 IPthree USERtwo
token1 IPthree USERthree
token1 IPtwo USERone
token1 IPtwo USERtwo
token1 IPtwo USERthree

C:\Users\Antonio\Documents\Test> type Ftp_logs\Three.log
token1 IPone USERone
token1 IPone USERtwo
token1 IPone USERthree
token1 IPthree USERone
token1 IPthree USERtwo
token1 IPthree USERthree
token1 IPthree USERone
token1 IPthree USERtwo
token1 IPthree USERthree

C:\Users\Antonio\Documents\Test> test

C:\Users\Antonio\Documents\Test> type ftp_ips.txt
IPone USERone
IPone USERtwo
IPone USERthree
IPtwo USERone
IPtwo USERtwo
IPtwo USERthree
IPthree USERone
IPthree USERtwo
IPthree USERthree
非常重要:在您在此发布任何进一步的评论之前,您必须复制我在测试中使用的数据,并使用相同的数据运行程序。之后,如果程序仍然没有使用真实数据运行,则必须比较两个数据以找出差异,并在下一条注释(如果有)中发布此信息


最后,你永远不应该因为答案不起作用就否决任何答案,特别是如果你没有提供任何有助于解决问题的反馈。请记住,您请求我们的帮助,我们是免费提供的…

您可以在生成的文件上使用uniq之类的工具,但文本示例将帮助您提供准确的解决方案。您好。谢谢,似乎正在工作,但正如foxidrive所说,它似乎在无休止地循环,它复制了所有重复的条目-我只想要用户名/ip地址的一个组合,而不是每次它们出现在日志中,其中一些有许多条目的组合。嗨,Aacini。谢谢,但它似乎没有更好的效果,它仍然会创建重复项,而且,它会不断重复以下错误:系统无法找到指定的路径。系统找不到指定的路径。系统找不到指定的路径。系统找不到指定的路径。“-]”未被识别为内部或外部命令、可操作程序或批处理文件。“-]”未被识别为内部或外部命令、可操作程序或批处理文件。系统找不到指定的路径。系统找不到指定的路径。您好,Aacini。真的很抱歉投了反对票,我从来没有这样做过,因为我请求帮助,那太粗鲁了。这是一个错误-我想投票,但按下了错误的按钮,当我试图纠正它,它不想让我。我会用我的数据测试一下,然后再给你回复。另外,我检查了路径,它们都很好,所以我不知道为什么会出现这些错误。非常感谢您迄今为止的帮助。
token1 IPone USERone
token1 IPone USERtwo
token1 IPtwo USERone
token1 IPthree USERtwo
C:\Users\Antonio\Documents\Test> dir
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Users\Antonio\Documents\Test

26/08/2015  10:49 a. m.    <DIR>          .
26/08/2015  10:49 a. m.    <DIR>          ..
26/08/2015  10:33 a. m.    <DIR>          Ftp_logs
26/08/2015  10:44 a. m.               414 test.bat
               1 archivos            414 bytes
               3 dirs  421,700,231,168 bytes libres

C:\Users\Antonio\Documents\Test> dir Ftp_logs
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Users\Antonio\Documents\Test\Ftp_logs

26/08/2015  10:33 a. m.    <DIR>          .
26/08/2015  10:33 a. m.    <DIR>          ..
26/08/2015  10:32 a. m.               204 One.log
26/08/2015  10:34 a. m.               216 Three.log
26/08/2015  10:33 a. m.               210 Two.log
               3 archivos            630 bytes
               2 dirs  421,700,231,168 bytes libres

C:\Users\Antonio\Documents\Test> type Ftp_logs\One.log
token1 IPone USERone
token1 IPone USERtwo
token1 IPone USERthree
token1 IPtwo USERone
token1 IPtwo USERtwo
token1 IPtwo USERthree
token1 IPone USERone
token1 IPone USERtwo
token1 IPone USERthree

C:\Users\Antonio\Documents\Test> type Ftp_logs\Two.log
token1 IPtwo USERone
token1 IPtwo USERtwo
token1 IPtwo USERthree
token1 IPthree USERone
token1 IPthree USERtwo
token1 IPthree USERthree
token1 IPtwo USERone
token1 IPtwo USERtwo
token1 IPtwo USERthree

C:\Users\Antonio\Documents\Test> type Ftp_logs\Three.log
token1 IPone USERone
token1 IPone USERtwo
token1 IPone USERthree
token1 IPthree USERone
token1 IPthree USERtwo
token1 IPthree USERthree
token1 IPthree USERone
token1 IPthree USERtwo
token1 IPthree USERthree

C:\Users\Antonio\Documents\Test> test

C:\Users\Antonio\Documents\Test> type ftp_ips.txt
IPone USERone
IPone USERtwo
IPone USERthree
IPtwo USERone
IPtwo USERtwo
IPtwo USERthree
IPthree USERone
IPthree USERtwo
IPthree USERthree