0

使用批处理文件,我需要将字符插入现有文本文件的每一行。将结果写入将用于填充数据库的新文本文件。插入的字符将成为分隔符,以正确排列数据。

示例:
当前存在的文件:

AAAAAAAAA~ZZZ  
AAAAAAAAA~BB~ZZZ  
AAAAAAAAA~BB~CCCC~ZZZ  
AAAAAAAAA~BBB~CC~ZZZ  

期望的结果:

AAAAAAAAA~~~~ZZZ  
AAAAAAAAA~BB~~~ZZZ  
AAAAAAAAA~BB~CCCC~~ZZZ  
AAAAAAAAA~BBB~CC~~ZZZ 

触发分隔符后的外观:

Part Num    Rev    Stat   Type        
AAAAAAAAA                 ZZZ  
AAAAAAAAA   BB            ZZZ  
AAAAAAAAA   BB     CC     ZZZ  
AAAAAAAAA   BBB    CC     ZZZ  

注意:
第一个字段总是 9 个字符
最后一个字段总是 3 个字符
我使用波浪号作为字段分隔符

4

1 回答 1

0

这应该这样做。
用法:mapper {sourfile} {targetfile}

mapper.cmd

@echo off
setlocal enabledelayedexpansion
if "%~1" == "" (
  echo No source file specified.
  goto :eof
)
if not exist "%~1" (
  echo Source file not found.
  goto :eof
)
if "%~2" == "" (
  echo No target file specified.
  goto :eof
)
if exist "%~2" (
  echo Target file is already exist.
  goto :eof
)
rem>"%~2"
for /f "tokens=1,2,3,4,5 delims=~" %%a in (%~1) do (
  if "%%b" == "" (
    echo %%a~~~~>>"%~2"
  ) else (
    if "%%c" == "" (
      echo %%a~~~~%%b>>"%~2"
    ) else (
      if "%%d" == "" (
        echo %%a~%%b~~~%%c>>"%~2"
      ) else (
        if "%%e" == "" (
          echo %%a~%%b~%%c~~%%d>>"%~2"
        ) else (
          echo %%a~%%b~%%c~%%d~%%e>>"%~2"
        )
      )
    )
  )
)
于 2012-07-20T09:28:09.397 回答