批处理脚本 - 逐行读取

发布于 2024-10-09 06:25:26 字数 359 浏览 0 评论 0原文

我有一个日志文件,需要逐行读入并将该行传送到下一个循环。

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

日志看起来像

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

使用 for /f 循环,我只获取 IP 而不是完整的行。

如何管道/写入/缓冲整行? (每行写什么并不重要)

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

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.

The log looks like

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏大泽ㄣ 2024-10-16 06:25:26

试试这个:

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

由于 tokens=* 所有内容都被捕获到 %a

edit 中:
要回复您的评论,您必须这样做:

@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 ...,所以整行。

Try this:

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

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

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.

温柔女人霸气范 2024-10-16 06:25:26

“调用”解决方案存在一些问题。

它因许多不同的内容而失败,因为 CALL 的参数被解析器解析了两次。
这些行或多或少会产生奇怪的问题

one
two%222
three & 333
four=444
five"555"555"
six"&666
seven!777^!
the next line is empty

the end

因此您不应该在调用中使用 %%a 的值,最好将其移至变量,然后仅使用变量名称调用函数。

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
    set "myVar=%%a"
    call :processLine myVar
)
goto :eof

:processLine
SETLOCAL EnableDelayedExpansion
set "line=!%1!"
set "line=!line:*:=!"
echo(!line!
ENDLOCAL
goto :eof

The "call" solution has some problems.

It fails with many different contents, as the parameters of a CALL are parsed twice by the parser.
These lines will produce more or less strange problems

one
two%222
three & 333
four=444
five"555"555"
six"&666
seven!777^!
the next line is empty

the end

Therefore you shouldn't use the value of %%a with a call, better move it to a variable and then call a function with only the name of the variable.

@echo off
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq delims=" %%a in (`"findstr /n ^^ t.txt"`) do (
    set "myVar=%%a"
    call :processLine myVar
)
goto :eof

:processLine
SETLOCAL EnableDelayedExpansion
set "line=!%1!"
set "line=!line:*:=!"
echo(!line!
ENDLOCAL
goto :eof
抽个烟儿 2024-10-16 06:25:26

这在过去对我有用,如果可以的话,它甚至会扩展文件中的环境变量。

for /F "delims=" %%a in (LogName.txt) do (
     echo %%a>>MyDestination.txt
)

This has worked for me in the past and it will even expand environment variables in the file if it can.

for /F "delims=" %%a in (LogName.txt) do (
     echo %%a>>MyDestination.txt
)
断舍离 2024-10-16 06:25:26

对于那些路径中有空格的人,您将需要这样的东西:
nb 它扩展为绝对路径,而不是相对路径,因此如果您的运行目录路径中有空格,这些也算在内。

set SOURCE=path\with spaces\to\my.log
FOR /F "usebackq delims=" %%A IN ("%SOURCE%") DO (
    ECHO %%A
)

解释一下:

(path\with spaces\to\my.log)

不会解析,因为空格。
如果变成:

("path\with spaces\to\my.log")

它将被处理为字符串而不是文件路径。

"usebackq delims=" 

查看文档将允许路径用作路径(感谢 Stephan)。

For those with spaces in the path, you are going to want something like this:
n.b. It expands out to an absolute path, rather than relative, so if your running directory path has spaces in, these count too.

set SOURCE=path\with spaces\to\my.log
FOR /F "usebackq delims=" %%A IN ("%SOURCE%") DO (
    ECHO %%A
)

To explain:

(path\with spaces\to\my.log)

Will not parse, because spaces.
If it becomes:

("path\with spaces\to\my.log")

It will be handled as a string rather than a file path.

"usebackq delims=" 

See docs will allow the path to be used as a path (thanks to Stephan).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文