批处理脚本 - 逐行读取 [英] batch script - read line by line

查看:43
本文介绍了批处理脚本 - 逐行读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志文件,我需要逐行读入并将该行通过管道传输到下一个循环.

I have a log file which I need to read in, line by line and pipe the line to a next loop.

首先,我在一个单独的文件中 grep 日志文件中的主要"字(如错误") - 以保持它小.现在我需要获取单独的文件并逐行读取它 - 每行都需要进入另一个循环(在这些循环中,我对日志进行 grep 并将其划分为块)但我卡在这里.

Firstly I grep the logfile for the "main" word (like "error") in a separate file - to keep it small. Now I need to take the seperate file and read it in line by line - each line needs to go to another loop (in these loop I grep the logs and divide it in blocks) but I stuck here.

日志看起来像

xx.xx.xx.xx - - "http://www.blub.com/something/id=?searchword-yes-no" 200 - "something_else"

使用 for/f 循环我只得到 IP 而不是完整的行.

with a for /f loop I just get the IP instead of the complete line.

如何管道/写入/缓冲整行?(每行写什么无关紧要)

How can I pipe/write/buffer the whole line? (doesn't matter what is written per line)

推荐答案

试试这个:

@echo off
for /f "tokens=*" %%a in (input.txt) do (
  echo line=%%a
)
pause

因为 tokens=* 一切都被捕获到 %a

because of the tokens=* everything is captured into %a

编辑:要回复您的评论,您必须这样做:

edit: to reply to your comment, you would have to do that this way:

@echo off
for /f "tokens=*" %%a in (input.txt) do call :processline %%a

pause
goto :eof

:processline
echo line=%*

goto :eof

:eof

因为有空格,你不能使用 %1,因为它只包含第一个空格之前的部分.并且由于该行包含引号,因此您也不能将 :processline "%%a"%~1 结合使用.所以你需要使用 %* 得到 %1 %2 %3 ...,所以整行.

Because of the spaces, you can't use %1, because that would only contain the part until the first space. And because the line contains quotes, you can also not use :processline "%%a" in combination with %~1. So you need to use %* which gets %1 %2 %3 ..., so the whole line.

这篇关于批处理脚本 - 逐行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆