使用批处理文件替换csv文件中的字符串 [英] replace string in csv file using batch file

查看:261
本文介绍了使用批处理文件替换csv文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含18个字段的csv文件.我需要将文件复制到txt文件,删除前四行,替换字段#8中的数据,然后使用新名称保存文件.
字段#8中的数据是整数(例如1、2、3等).每个整数都需要用一个单独的值替换(例如,我需要将1替换为1005,将3替换为1008).我正在尝试修改/修复以下批处理文件:

I have a csv file with 18 fields. I need to copy the file to a txt file, delete the first four lines, replace the data in field #8, and save the file with a new name.
The data in field #8 is an integer (for example, 1, 2, 3, etc). Each integer needs to be replaced with a separate value (for example, I need to replace 1 with 1005 and 3 with 1008). I am trying to modify/fix the following batch file:

@echo off
More +4 datatest.csv > datacopy.txt
( FOR /f "tokens=8 delims=," %%h in (datacopy.txt) do (
    if "%%h"=="3" (echo 1008) else ( 
      echo %%a %%b %%c`  echo %%a %%b %%c
    ) 
  )
)>paygoinvoice.txt
@echo on

推荐答案

  • 仅选择一个令牌,您将只获得一列(%% h)
  • 直接解析more命令,不需要临时文件.
  • 根据要替换的整数数量,您可以使用带有int的伪数组作为索引/指针.
  • 您可以分别获取所有列(tokens=1-18%%A..%%R),也可以将其余的*收集到一个变量中.
    • With only one token selected, you'll get only one column (%%h)
    • parsing the more command directly, there is no need for a temporary file.
    • depending on how many integers to replace, you may use a pseudo array with the int as an index/pointer.
    • you may either get all columns separately (tokens=1-18,%%A..%%R) or gather the rest * in one for variable.
    • @echo off & Setlocal EnableDelayedExpansion
      ( FOR /f "tokens=1-8* delims=," %%A in ('More +4 datatest.csv') do (
          Set "H=%%H"
          if "%%H"=="1" Set "H=1005"
          if "%%H"=="3" Set "H=1008"
          echo %%A,%%B,%%C,...,!H!,%%I
        )
      )>paygoinvoice.txt
      @echo on
      

      这篇关于使用批处理文件替换csv文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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