如何在读取文件时删除多个选项卡(cmd)

时间:2012-04-02 10:45:48

标签: batch-file cmd readfile

请帮忙解决这个问题。

我的文件是这样的: http://www.crocko.com/9A60004ABE6046578ED113CFFFB75F6E/activate_all_regions.txt (请点击右下角。有计时器,但我没有找到没有计时器的上传服务器。在文件中有制表符,这就是为什么我把它上传到服务器上)

这是阅读它的脚本:

    @echo off
setlocal EnableDelayedExpansion

if "%1"=="" (
  IF EXIST "..\_install\activate all regions.txt" (
    SET FILE="..\_INSTALL\activate all regions.txt"
    echo Setting !FILE! as your configuration file
  ) ELSE (
   echo File not found. First argument is name of configuration file .txt.
   pause
  )
) else ( SET "FILE=%1")

for /F "skip=4 delims=pR tokens=1,2" %%a in ( 'reg query hkcu\environment /v temp' ) do set TAB=%%b

FOR /F "usebackq delims=!TAB! tokens=1-3" %%L IN (!FILE!) DO (
for /F "delims=!TAB! tokens=1-3" %%a in ("%%L") do echo A:"%%a"B:"%%b"C:"%%c"
REM echo %%L
)

我希望输出如下:

A:"Directory name 1"B:"1"C:"Description to this line" 
A:"Directory name 2"B:"1"C:"Description to this line" 
A:"Directory name xxx"B:"1"C:"Description to this line" 

目标是过滤重复的标签(标签)(标签)(标签)(标签)并替换为一个标签(标签)。 所以第一步需要导致columnA(tab)columnB(tab)columnC(tab),然后在第二步中解析它以将列变为三个变量。

1 个答案:

答案 0 :(得分:1)

!TAB!在for命令中必须是%TAB%:

@echo off
setlocal EnableDelayedExpansion

for /F "skip=4 delims=pR tokens=1,2" %%a in ( 'reg query hkcu\environment /v temp' ) do set TAB=%%b

SET FILE="activate all regions.txt"

FOR /F "usebackq tokens=1-3 delims=%TAB%" %%a IN (!FILE!) DO (
   echo A:"%%a" B:"%%b" C:"%%c"
)

结果:

A:"all_in_one" B:"1" C:""
A:"Ji×nÝ ╚echy st°ed" B:"1" C:""
A:"Ji×nÝ ╚echy v²chod - ╚B" B:"1" C:""
A:"Ji×nÝ ╚echy zßpad" B:"1" C:""
A:"Ji×nÝ Morava st°ed 1 - Znojmo" B:"1" C:""
A:"Ji×nÝ Morava st°ed 2 - Brno, B°eclav" B:"1" C:"Oblast od Brna a× po B°eclav."
A:"Ji×nÝ Morava v²chod 1 (Uh.Hrad.)" B:"1" C:""
A:"Ji×nÝ Morava v²chod 2 (Ji×nÝ Beskydy)" B:"1" C:""
A:"Ji×nÝ Morava zßpad - TelÞ" B:"1" C:""
A:"SevernÝ ╚echy" B:"1" C:""
A:"SevernÝ Morava v²chod - Ostravsko" B:"1" C:"Oblast zahrnuje Opavsko, Ostravsko, Okres Karvinß a Fr²dek MÝstek"
A:"SevernÝ Morava zßpad - Jesenicko" B:"1" C:"Oblast zahrnuje JesenÝk, Krnov, Bruntßl, R²ma°ov, Olomouc"
A:"St°ednÝ ╚echy 1 jih" B:"1" C:""
A:"St°ednÝ ╚echy 1 sever" B:"1" C:""
A:"St°ednÝ ╚echy 1 st°ed" B:"1" C:""
A:"St°ednÝ ╚echy 2 jih" B:"1" C:""
A:"St°ednÝ ╚echy 2 sever" B:"1" C:""
A:"St°ednÝ ╚echy 2 st°ed" B:"1" C:""
A:"V²chodnÝ ╚echy jih" B:"1" C:""
A:"ZßpadnÝ ╚echy jih" B:"1" C:""
A:"ZßpadnÝ ╚echy sever" B:"1" C:""
A:"ZßpadnÝ ╚echy st°ed (K.Vary)" B:"1" C:""
A:"ZßpadnÝ Morava" B:"1" C:"Oblast zahrnuje Prostýjov, Zßb°eh, èumperk"