是否有可能在一个批处理文件,由线管路读? [英] Is it possible in a batch file to read from a pipe line by line?

查看:143
本文介绍了是否有可能在一个批处理文件,由线管路读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能从管在批处理文件中读取。如果我写:

I was wondering if it is possible to read from a pipe in a batch file. If I write:

echo Test

我得到的,不足为奇,测试。那很好。但是,如果我希望通过管道输出,并从另一个命令读什么?

i get, unsurprising, Test. That's nice. But what if I want to pipe the output, and read it from another command?

echo Test | echo ???

如何像以前获得相同的结果,但是通过管?
谢谢!

How to obtain the same result as before, but through a pipe? Thanks!

编辑:什么我真的后之后是这样的。

what I am after really after is this.

我有一个文件列表,我需要一些的话,我说,一行行,在一个名为 filter.txt 来过滤此列表。所以,我必须使用 FINDSTR /g:filter.txt

I have a list of files, and i need to filter this list with some words that i put, line by line, in a file named filter.txt. So I have to use findstr /g:filter.txt.

但后来我需要做一些事来匹配列表中的文件,因为 FINDSTR 返回一行为每个文件,我要读一行比赛线路。

But then I need to do something to the list files that matches, and since findstr returns one row for each file, i have to read the matches line by line.

这是我怎么做的:

dir /b | findstr /g:filter.txt | for /F "delims=" %a in ('more') do del "%a"

解决方案:

它看起来像什么我想要做的是没有的从管道读的只是读另一个命令的输出在一个批处理文件。

It looks like that what I wanted to do was not reading from a pipe but just reading the output of another command in a batch file.

要做到一行读取,你可以使用这样的:

To do a single line read, you could use this:

echo Test | ( set /p line= & call echo %%line%%)

或者你可以利用这一点,与多线路输入也可以工作:

or you can use this, that works also with multi line input:

echo Test | for /F "delims=" %a in ('more') do @echo %a

(这一招使用的更多的可能是在某些情况下非常有用)。但是,在我的具体情况,解决的办法是这样的:

(this trick of using more could be useful in some situations). But in my particular case, the solution is this:

for /F "delims=" %a in ('echo Test') do @echo %a

谢谢大家!

推荐答案

对不起,我认为这里有一个困惑...

Excuse me, I think there is a confusion here...

您说您要的从管道的读取。一个管道是用来重定向一个命令的输出到另一个命令的输入;第二个命令被称为的过滤器的。例如,在

You said you want to read from a pipe. A pipe is used to redirect the output of one command into the input of another command; the second command is called filter. For example, in

dir /b | findstr /g:filter.txt

DIR FINDSTR 命令之间的管道。一个管道总是两个进程之间建立的。有没有办法读取从 DIR 命令流向 FINDSTR 数据命令(也就是唯一存在的管这里)。但是,你可以从输出 FINDSTR 命令读取。

there is a pipe between dir and findstr commands. A pipe is always established between two processes. There is no way to read the data that flow from dir command to findstr command (that is the only pipe that exist here). However, you can read from the output of findstr command.

如果我们插入一个额外的过滤器,行为是一样的。例如,在

If we insert an additional filter, the behavior is the same. For example, in

dir /b | findstr /g:filter.txt | more

有两个管道,但也没有办法从他们的任何人阅读。但是,您可以从最后一个命令的(更多在这种情况下)的输出读取。什么是原生批量解决阅读的输出的一个命令的?它是FOR / F命令。例如,原生的方式来获得回声命令的输出:

there are two pipes, but there is no way to read from anyone of them. However, you can read from the output of the last command (more in this case). What is the native Batch solution to read the output of one command? It is the FOR /F command. For example, the native way to get echo command output in:

echo Test | for /F "delims=" %a in ('more') do @echo %a

是:

for /F "delims=" %a in ('echo Test') do @echo %a

请注意,在第一个例子中的%的参数没有得到从管道将回声命令,但是从的输出更多命令。

Please note that in the first example the %a parameter does NOT get the information from the pipe that exist between echo and for commands, but from the output of more command.

在以相同的方式,自然的方法来实现此任务

In the same way, the natural method to achieve this task:

dir /b | findstr /g:filter.txt | for /F "delims=" %a in ('more') do del "%a"

是这样的:

for /F "delims=" %a in ('dir /b ^| findstr /g:filter.txt') do del "%a"

这是处理 FINDSTR 命令的多行输出。

that process the multi-line output of findstr command.

第二种方法不只是快于前者,但它也是清晰的,因为更多命令列入真的什么都不做可能会导致不希望的误解或错误。

Second method is not just faster than the former, but it is also clearer because the inclusion of a more command that really do nothing may lead to undesired misconceptions or errors.

安东尼奥

这篇关于是否有可能在一个批处理文件,由线管路读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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