如何从逗号分隔文件中删除前导零

Mik*_*ike 3 csv batch-file

我有一个文件(file1.csv)有两个逗号分隔的值.我需要一种方法从一个批处理文件中的两个值中删除前导零.

到目前为止我用过这个:

@echo off
(for /f "tokens=*,* delims=0" %%a in (file1.csv) do echo(%%a)>stripped.txt
Run Code Online (Sandbox Code Playgroud)

它工作得很好,虽然它只从第一个数字而不是第二个数字中删除零.

样本来自file1.csv:

00012345,00000012345 

00067890,00000067890
Run Code Online (Sandbox Code Playgroud)

stripped.txt使用上述批处理文件后的示例:

12345,00000012345

67890,00000067890
Run Code Online (Sandbox Code Playgroud)

任何人都有关于我如何对逗号后面的数字做同样的建议?

dbe*_*ham 5

如果您愿意使用名为REPL.BAT的混合JScript /批处理实用程序,那么解决方案可以简单如下:

type file.csv|repl "0*(\d\d*),0*(\d\d*)" "$1,$2" >stripped.csv
Run Code Online (Sandbox Code Playgroud)

这个REPL.BAT解决方案不仅简单,而且效率也很高.它可以非常快速地处理大型CSV.

如果你必须有一个纯批处理解决方案,那么这里是一个不使用CALL或GOTO或延迟扩展,并且它正确处理值为0.这将比REPL.BAT解决方案慢得多,但我认为它是最有效的纯批量解决方案.

第一个循环将该行解析为两个值.

然后每个值还有两个循环.第一个删除前导零,但它还在空格后附加一个额外的0值,这样它总是返回一个字符串,即使该值为0.最后一个循环然后返回原始的零剥离值,或者如果它已经因为它是0而被删除,然后它返回附加的0值.

@echo off
(for /f "delims=, tokens=1,2" %%A in (file.csv) do (
  for /f "delims=0 tokens=*" %%C in ("%%A 0") do for /f %%E in ("%%C") do (
    for /f "delims=0 tokens=*" %%D in ("%%B 0") do for /f %%F in ("%%D") do (
      echo %%E,%%F
    )
  )
))>stripped.csv
Run Code Online (Sandbox Code Playgroud)

剥离前导零的代码可以封装在一个函数中,然后使用起来变得更加方便.如果要删除的行中有许多值,则尤其如此.但是CALL机制非常缓慢.对于这两个要剥离的值的简单问题,它会使解决方案的速度降低5倍以上.

@echo off
setlocal enableDelayedExpansion

(for /f "delims=, tokens=1,2" %%A in (file.csv) do (
  call Strip0 %%A A
  call Strip0 %%B B
  echo !A!,!B!
))>stripped.csv
exit /b

:strip0  ValueStr  [RtnVar]
::
:: Strip leading zeros from value ValueStr and store the result in vaiable RtnVar.
:: If RtnVar is not specified, then print the result to stdout.
::
for /f "delims=0 tokens=*" %%A in ("%~1") do for /f %%B in ("%%A 0") do (
  if "%~2" equ "" (echo %%B) else set "%~2=%%B"
)
exit /b
Run Code Online (Sandbox Code Playgroud)

有一种高级批处理宏技术可以将逻辑封装在宏函数中,而不会显着减慢速度.有关带参数的批处理宏的背景信息,请参见http://www.dostips.com/forum/viewtopic.php?f=3&t=1827.

这是使用批处理宏的解决方案.它比CALL方法快4倍.

@echo off

:: The code to define the macro requires that delayed expansion is disabled.
setlocal disableDelayedExpansion
call :defineStrip0

:: This example requires delayed expansion within the loop
setlocal enableDelayedExpansion
(for /f "delims=, tokens=1,2" %%A in (file.csv) do (
  %strip0% %%A A
  %strip0% %%B B
  echo !A!,!B!
))>stripped.csv
exit /b


:defineStrip0    The code below defines the macro.

:: Define LF to contain a linefeed character (0x0A)
set ^"LF=^

^" The above empty line is critical - DO NOT REMOVE

:: Define a newline with line continuation
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

::%strip0%  ValueStr  [RtnVar]
::
::  Strip leading zeros from string ValueStr and return the result in variable StrVar.
::  If RtnVar is not specified, then print the result to stdout.
::
set strip0=%\n%
for %%# in (1 2) do if %%#==2 (setlocal enableDelayedExpansion^&for /f "tokens=1,2" %%1 in ("!args!") do (%\n%
  for /f "delims=0 tokens=*" %%A in ("%%1") do for /f %%B in ("%%A 0") do (%\n%
    endlocal^&if "%%2" equ "" (echo %%B) else set "%%2=%%B"%\n%
  )%\n%
)) else set args=
exit /b
Run Code Online (Sandbox Code Playgroud)