Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 管道输出到批处理脚本_Batch File_Pipe - Fatal编程技术网

Batch file 管道输出到批处理脚本

Batch file 管道输出到批处理脚本,batch-file,pipe,Batch File,Pipe,我正在windows上使用grep筛选文件,并希望通过管道将输出传输到另一个批处理脚本 e、 g 在mybatch中,我希望处理每个匹配行 e、 g.mybatch.bat: echo Line with foo: %1 在linux上,我可以通过 grep "foo" bar.txt | while read x; do mybatch.sh $x; done 但我不知道如何在windows机器上实现这一点 提前谢谢 您可以将用于: for /f %x in ('grep "foo" ba

我正在windows上使用grep筛选文件,并希望通过管道将输出传输到另一个批处理脚本

e、 g

在mybatch中,我希望处理每个匹配行

e、 g.mybatch.bat:

echo Line with foo: %1
在linux上,我可以通过

grep "foo" bar.txt | while read x; do mybatch.sh $x; done
但我不知道如何在windows机器上实现这一点


提前谢谢

您可以将
用于

for /f %x in ('grep "foo" bar.txt') do call mybatch.cmd "%x"

这将为每一个非空行调用一次批处理文件,就像您的shell脚本示例所做的那样。

以下是交易技巧:

蝙蝠头

@echo off

setlocal ENABLEDELAYEDEXPANSION
set n=%1

for /F "tokens=*" %%x in ('more') do (
echo %%x
set /A n -= 1
if !n! EQU 0 exit /b
)
使用它:

REM print the first 10 lines:
type test.txt | head 10 

谢谢,效果很好,我将我的管道链输出到一个临时文件中,然后在临时文件上执行for循环(发生在另一个批处理文件中):for/f“tokens=*”%%x in('type temp.tmp')do call test.cmd“%%x”,但它会在传递到test.cmd的每一行中添加引号,因此如果test.cmd的回显行是%1,我将得到的行是“foo”(带qout)而临时文件中的行没有。有什么技巧可以去掉引号吗?改用
%~1
。但是没有必要求助于
类型
,在这种情况下,
对于/f%%x in(temp.tmp)执行…
执行相同的操作。这不是管道。。。这项任务可能已经完成,但问题是否仍然没有答案?(编辑:建议重命名该问题)
REM print the first 10 lines:
type test.txt | head 10