【问题标题】:read then write delimited file - Batch cmd读取然后写入分隔文件 - 批处理 cmd
【发布时间】:2014-09-18 07:04:47
【问题描述】:

您好,我有一个管道 | 分隔文件。 我需要把里面的所有数字都倒过来

文件如下所示

Entity|Division|Channel|200|300|800

我需要读取文件并创建一个带有相反数字的新文件

Entity|Division|Channel|-200|-300|-800

试图完成这项工作,但不完全确定如何修改我得到的文本。我需要有关:processToken 程序的帮助。如何将标记输出到新文件并添加“-”并添加回分隔符|

for /f "tokens=* delims= " %%f in (M:\GPAR\Dev\EQ\Upload_Files\eq_test.txt) do (
set line=%%f
call :processToken
)
goto :eof

:processToken
for /f "tokens=* delims=|" %%a in ("%line%") do (
)
if not "%line%" == "" goto :processToken
goto :eof

谢谢

【问题讨论】:

    标签: windows batch-file csv


    【解决方案1】:
    @echo off
    set "ent_file=c:\entity.txt"
    break>"%tmp%\rev.txt"
    for /f "usebackq tokens=1,2,3,4,5,6 delims=|" %%a in ("%ent_file%") do (
    
        echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f
    
    )>>"%tmp%\rev.txt"
    
    move /y "%tmp%\rev.txt" "%ent_file%" 
    del /q /f "%tmp%\rev.txt"
    

    如果您的文件只包含您发布的那一行,则应该可以使用。

    EDIT输出第6个token之后的部分:

    @echo off
    set "ent_file=c:\entity.txt"
    break>"%tmp%\rev.txt"
    for /f "usebackq tokens=1,2,3,4,5,6,* delims=|" %%a in ("%ent_file%") do (
    
        echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f|%%g
    
    )>>"%tmp%\rev.txt"
    
    move /y "%tmp%\rev.txt" "%ent_file%" 
    del /q /f "%tmp%\rev.txt"
    

    【讨论】:

    • 谢谢你的作品。我以为我很聪明,看看我是否可以将它应用于超过 6 列。 26/Z 之后会发生什么?我总共有 55 列。
    • @gemmo 你可以使用 61 个标记 ->ss64.com/nt/for_f.html - 但你需要知道它们的顺序。这里是它们的全部 ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ `` a b c d e f g h i j k l m n o p q r s t u v w x y z { 注意插入符号 ^ 必须用第二个插入符号转义: ^^
    • @gemmo - 检查这个stackoverflow.com/questions/8520313/… 事实证明你不能获得超过 31 个令牌。
    • @gemmo ,如果要处理直到第 6 个令牌的字符串,可以使用 * 并将其余部分分配给 %%g
    【解决方案2】:
    @echo off
    setlocal EnableDelayedExpansion
    
    set digits=0123456789
    
    (for /F "delims=" %%f in (eq_test.txt) do (
       set "input=%%f"
       set "output="
       call :processToken
       set /P "=!output:~1!" < NUL
       echo/
    )) > new_file.txt
    goto :EOF
    
    :processToken
       for /F "tokens=1* delims=|" %%a in ("%input%") do (
          set "token=%%a"
          set "input=%%b"
       )
       if "!digits:%token:~0,1%=!" neq "%digits%" set "token=-%token%"
       set "output=%output%|%token%"
       if defined input goto processToken
    exit /B
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      • 2011-02-14
      • 2021-07-30
      • 1970-01-01
      • 2016-01-31
      • 2020-06-15
      • 2013-09-21
      • 2014-05-30
      相关资源
      最近更新 更多