Replace specific text in csv via commandline

前端 未结 3 1590
孤街浪徒
孤街浪徒 2021-01-16 04:38

I have data in csv format that gets output from SQL Server. The data has some NULL and N.A. values written out which makes a column ch

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 05:34

    Try this out:

    @echo off
    setLocal enableDelayedExpansion
    
    set filename=input.txt
    set originalText1=NULL
    set "replacedText1=-1"
    set "originalText2=N.A."
    set "replacedText2=-2"
    
    for /f "tokens=*" %%a in ('type %filename%') do (
        set "line=%%a"
        if defined line (
            call set "line=%%line:%originalText1%=%replacedText1%%%"
            call set "line=%%line:%originalText2%=%replacedText2%%%"
            echo !line!>> output.txt
        ) else (
            echo.
        )
    )
    

    This code will help you to replace all the instance of NULL to -1 and N.A. to -2. The result will then be stored in output.txt. Hope it helps.

    P.S. Note that call set is needed as it will expand the variable that is passed on the same line.

提交回复
热议问题