以CMD反向输出 [英] Output in Reverse in CMD

查看:118
本文介绍了以CMD反向输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个短语需要在results.txt中输出。该短语来自x.txt。例如:我有两个孩子,它应该在results.txt中输出两个我有两个孩子。

I have a phrase that needs to be outputted in results.txt. The phrase comes from x.txt. For example: "I have two kids", it should output "kids two have I" in results.txt.

更新

它已经在工作,但我不想循环。
请参见下面的代码

Its already working but i want no loop. Pls see code below

代码

@ECHO OFF
set /p content=<x.txt
SET var=!content: =,!
SET rev=
:LOOP
IF NOT "!var!"=="" (
    FOR /F "delims=, tokens=1,*" %%F IN ("!var!") DO (
        SET rev=%%F,!rev!
        SET var=%%G
    )
) ELSE (
    SET rev=!rev:~0,-1!
    GOTO ENDLOOP
)
GOTO LOOP
:ENDLOOP
ECHO !rev:,= ! > results.txt


推荐答案

这是前四个字(文本文件的每一行),然后以相反的顺序将其重写为 result.txt

This takes the first four words (of each line) of the text file and rewrites them in reverse order to result.txt:

>result.txt (for /f "tokens=1-4" %%a in (x.txt) do echo %%d %%c %%b %%a)

另一种解决方案(对于单行文本文件,未指定字数):

Another solution (for a one-line text file, unspecified number of words):

@ECHO OFF
setlocal enabledelayedexpansion
echo I have two little but wonderful kids>x.txt
<x.txt set /p x=
for %%a in (%x%) do set "res=%%a !res!"
>result.txt echo %res:~0,-1%

(尽管从技术上讲, for 命令 本身是一个循环)

(although technically, the for command is a loop on its own)

无任何形式一个循环,如果您可以在前面留一些空间:

Without any form of a loop, if you can live with some spaces at the front:

@ECHO OFF
setlocal 
echo I have two kids>x.txt
<x.txt set /p x=
call :reverse %x%
goto :eof
:reverse
set "rev=%9 %8 %7 %6 %5 %4 %3 %2 %1"
echo Reverse without any form of loop: "%rev%"
for /f "tokens=*" %%a in ("%rev%") do echo To get rid of the spaces, you need a FOR loop: "%%a"

由于 cmd 仅支持%1 %9

您可以通过 shift 命令,但这意味着要使用循环。

This is limited to a maximum of nine words because cmd supports only %1to %9
You can use more parameters (words) by using the shift command, but that would mean using a loop.

这篇关于以CMD反向输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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