[英]How do you correctly handle redirection in FOR loop in one line of command?
I want to output the file cat.txt's content and save them in another text file using FOR /F loop ( in one line of command of standard cmd shell.) 我想输出文件cat.txt的内容,并使用FOR / F循环将它们保存在另一个文本文件中(在标准cmd shell的一行命令中。)
for example, here is the source file. 例如,这是源文件。
# content of cat.txt:
meow
meo
me
m
after use this command, I've got some unintended result: 使用此命令后,我得到了一些意外的结果:
FOR /F "tokens=*" %G in (cat.txt) DO echo %G > D:\\copy-cat.txt
# I got output:
meow > D:\copy-cat.txt
meo > D:\copy-cat.txt
me > D:\copy-cat.txt
m > D:\copy-cat.txt
I understand in this scenario, the redirection is being affected by echo, hence recognized as string and echoed out. 我了解在这种情况下,重定向受到回声的影响,因此被识别为字符串并被回声。
Question: How do I semantically separate the code so that I can redirect all the content that were written to stdout into D:\\copy-cat.txt? 问题:如何在语义上分离代码,以便可以将所有写到stdout的内容重定向到D:\\ copy-cat.txt?
Note: as you can see by %G, I need to write this directly in commandline, not in a batch script. 注意:如%G所示,我需要直接在命令行中而不是在批处理脚本中编写此代码。
>copy-cat.txt (FOR /F "delims=" %G in (cat.txt) DO @echo %G)
for
output for
输出 echo
command itself, use prefix the command with @
to not echo the echo
(single command echo off
) echo
命令本身,请在命令前使用@
前缀以不回显echo
(单个命令echo off
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.