简体   繁体   English

替换CSV字符串中的第n个元素

[英]Replace n-th element in CSV string

I try to replace the n-th element of a CSV string, without knowing his value. 我尝试替换CSV字符串的第n个元素,但不知道其值。 For example, here is my string : 例如,这是我的字符串:

*;*;*;element_to_replace;*;*

With * an undefined string, it can be anything. 使用*未定义的字符串,它可以是任何东西。

So i tried to use : 所以我尝试使用:

for /F "delims=" %%w in (file\workstation) do (
    set line=%%w
    if !compt! NEQ 0 (
        set new_line=!line:*;*;*;*=*;*;*;new_value!
        @echo !new_line! >> file\tmp_workstation
    ) else (
        @echo !header_workstation! >> file\tmp_workstation
    )
    set /A "compt+=1"
)

It doesn't work. 没用 Am i doing something wrong ? 难道我做错了什么 ?

@echo off 
setlocal enabledelayedexpansion
REM you want to replace token 4:
for /f "tokens=1-4,* delims=;" %%a in (t.csv) do (
   echo %%a;%%b;%%c;replaced;%%e
)

tokens=1-4,* means: take the first four tokens, the fifth token is "the rest of the line". tokens=1-4,*表示:采用前四个令牌,第五个令牌为“该行的其余部分”。 %%a is the first token, %%b is the second one etc. %%a是第一个令牌, %%b是第二个令牌, %%b
You want to write token1;token2;token3,"replacement string for the fourth token(%%d)";"rest of the line" (fifth token) . 您要编写token1;token2;token3,"replacement string for the fourth token(%%d)";"rest of the line" (fifth token)

Supposing the * characters do not appear literally within your data and it does also not contain any ? 假设*字符没有真正出现在您的数据中,并且也不包含任何? marks, you could use the following code snippet: 标记,您可以使用以下代码段:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "INFILE=file\workstation"
set "OUTFILE=file\tmp_workstation"
set "SEPARATOR=;"
set /A "COL_NUM=4"
set "COL_NEWVAL=new_value"

rem // A single redirection:
> "%OUTFILE%" (
    set "HEADER=#"
    rem // Read CSV file line by line:
    for /F usebackq^ delims^=^ eol^= %%L in ("%INFILE%") do (
        set "LINE=%%L"
        if defined HEADER (
            rem // Skip header from replacement:
            set "NEW_LINE=%%L"
            set "HEADER="
        ) else (
            set "NEW_LINE=" & set "SEP=" & set /A "IDX=0"
            rem // Toggle delayed expansion to not lose any `!`:
            setlocal EnableDelayedExpansion
            set "LINE=!LINE:"=""!^"
            rem // Use standard `for` loop to enumerate column values:
            for %%I in ("!LINE:%SEPARATOR%=","!") do (
                endlocal
                set /A "IDX+=1"
                set "ITEM=%%~I"
                setlocal EnableDelayedExpansion
                rem // Replace column value if index matches:
                if !IDX! EQU %COL_NUM% (
                    endlocal
                    set "ITEM=%COL_NEWVAL%"
                    setlocal EnableDelayedExpansion
                ) else (
                    if defined ITEM set "ITEM=!ITEM:""="!^"
                )
                rem /* Collect line string;
                rem    `for /F` loop to pass string beyond `endlocal` barrier: */
                for /F delims^=^ eol^= %%E in ("!NEW_LINE!!SEP!!ITEM!") do (
                    endlocal
                    set "NEW_LINE=%%E"
                    setlocal EnableDelayedExpansion
                )
                endlocal
                set "SEP=%SEPARATOR%"
                setlocal EnableDelayedExpansion
            )
            endlocal
        )
        rem // Output newly built line:
        setlocal EnableDelayedExpansion
        echo(!NEW_LINE!
        endlocal
    )
)

endlocal
exit /B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM