如何将多行从FINDSTR输出到变量 [英] How to output multiple lines from a FINDSTR to a variable

查看:48
本文介绍了如何将多行从FINDSTR输出到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毫不奇怪,官方文档在理解如何命令是否处理命令的结果而不是文件列表,为什么它甚至都不被称为"FOR".是的,我已经知道stackoverflow充满了类似的问题,但是显然,因为批处理脚本受很多因素的影响突破性"因素表明,即使作为一个不分批处理的经验丰富的程序员,也很难不迷失于成千上万的异常可能会影响结果.除了从最佳答案中学习之外,我的目标是提出一个足够通用的问题表示问题可能是最常见的任务,包括FINDSTR命令:

It's no surprise that the official documentation doesn't really help in the matter of understanding how does the command process the result of a command instead of a filelist neither why is it even called 'FOR'. Yes I already know stackoverflow is full of similar question but appearently, since batch scripts are influenced by so many "breaking" factors that, even as a non-batch experienced programmer, it is difficult not to get lost in the thousands exceptions and do-nots wich may affect the result. My objective, aside from learning from the best answer possible, is to formulate a generic enough question to represent the matter wich is probably the most common task including the FINDSTR command:

问题:

如何以某种方式获得FINDSTR的输出,该方式允许我一次计算每个结果行可能在循环内?

我可以做的最通用"(如果您了解我的意思,则为批量bs证明)示例:

The most 'generic' (batch bs-proof if you know what I mean) example I can make is the following:

让我们说这是secret_file.txt

Let's say this is secret_file.txt

some not intersting line
            A very intersting line = "secret1";
some not intersting line
            A very intersting line = "secret2";
some not intersting line
            A very intersting line = "secret3";
some not intersting line

现在使用findstr命令,我可以像这样输出每条秘密"行:

Now with the findstr command i can output every "secret" line like this:

findstr /R /C:"secret.\"" secret_file.txt
                                A very intersting line = "secret1";
                                A very intersting line = "secret2";
                                A very intersting line = "secret3";

但是,如果不进一步解析,此结果就毫无用处了吗?我本可以在任何文本阅读器/编辑器上使用ctrl-F对于这个问题,无论如何,我现在想一次输出每一行,以便我可以对其进行计算,例如,将每个机密保存到变量中,然后以某种方式使用该变量(实际上并不重要,我们可以回显它以保持简单).

But this result is just useless without futher parsing right? I could have used ctrl-F over any text reader/editor for this matter, anyway, let's say I now want to output every line ONE AT THE TIME so that I can compute it, for example, saving every secret to a variable then using that variable somehow (it doesn't really matter how, we can just echo it to keep things simple).

现在,每个人都同意这样一个事实,即需要执行FOR循环.引用 https://ss64.com/nt/for.html 的语法,我的脚本.bat应该如下所示:

Now, everybody agrees on the fact that for this kind of task, a FOR loop is needed. Quoting https://ss64.com/nt/for.html on the syntax, my script.bat should looks like this:

@echo off

FOR /F %%A IN ('findstr /R /C:"secret.\"" secret_file.txt') DO ECHO Batch script language is completely fine, good job Microsoft!

这甚至都没有给出任何输出,有人可以解释一下为什么吗?我的假设是findstr命令的输出与FOR命令的格式不兼容,因为源已关闭且文档甚至不必费心定义字符串"一词.我已经准备好提供任何详细信息,甚至可以编辑问题以使其更容易被看到被微软抛弃的批处理脚本编写者.

This just doesn't even give any output, can someone explain me why? My hypothesis was that the output from the findstr command is in a non-compatible format with the FOR command, not like I could check or something since the source is closed and the documentation doesn't even bother defining the word String. I'm ready to provide any details and even edit the question to be more visible to the wanna be microsoft-forsaken batch scripters out there.

推荐答案

使用"tokens = *" 去除前导空格,此批处理使用计数器创建(伪)数组秘密[]

Using "tokens=*" to strip off leading spaces this batch uses a counter to create a (pseudo) array secret[]

:: Q:\Test\2018\12\04\SO_53614102.cmd
@Echo off
set Cnt=0
FOR /F "tokens=*" %%A IN (
  'findstr /R /C:"secret.\"" secret_file.txt'
) DO (
  set /a Cnt+=1
  call Set Secret[%%Cnt%%]=%%A
)
Set Secret[

示例输出:

> SO_53614102.cmd
Secret[1]=A very intersting line = "secret1";
Secret[2]=A very intersting line = "secret2";
Secret[3]=A very intersting line = "secret3";

(在代码块中)变量在解析时被扩展,
需要延迟的扩展(在此处通过致电并加倍了%%)

As variables in a (code block) are expanded at parse time,
delayed expansion is requiered (here through a call and doubled %%)

这篇关于如何将多行从FINDSTR输出到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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