批处理脚本 - 逐行读取
我有一个日志文件,需要逐行读入并将该行传送到下一个循环。
首先,我在一个单独的文件中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
由于
tokens=*
所有内容都被捕获到%a
edit 中:
要回复您的评论,您必须这样做:
由于空格,您不能使用
%1
,因为它只包含第一个空格之前的部分。并且由于该行包含引号,因此您也不能将:processline "%%a"
与%~1
结合使用。所以你需要使用%*
来获取%1 %2 %3 ...
,所以整行。Try this:
because of the
tokens=*
everything is captured into%a
edit:
to reply to your comment, you would have to do that this way:
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.“调用”解决方案存在一些问题。
它因许多不同的内容而失败,因为
CALL
的参数被解析器解析了两次。这些行或多或少会产生奇怪的问题
因此您不应该在调用中使用
%%a
的值,最好将其移至变量,然后仅使用变量名称调用函数。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
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.这在过去对我有用,如果可以的话,它甚至会扩展文件中的环境变量。
This has worked for me in the past and it will even expand environment variables in the file if it can.
对于那些路径中有空格的人,您将需要这样的东西:
nb 它扩展为绝对路径,而不是相对路径,因此如果您的运行目录路径中有空格,这些也算在内。
解释一下:
不会解析,因为空格。
如果变成:
它将被处理为字符串而不是文件路径。
查看文档将允许路径用作路径(感谢 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.
To explain:
Will not parse, because spaces.
If it becomes:
It will be handled as a string rather than a file path.
See docs will allow the path to be used as a path (thanks to Stephan).