我有一個.csv包含 4 個欄位的檔案。
欄位 1、2、3 是文本框
欄位 4 是一個數字,例如 1、2、3 等。
對于多個記錄,欄位 1、2 和 3 有多個實體完全相同。在這些情況下,我想洗掉除其中一條記錄之外的所有記錄,并將其他(現已洗掉)記錄中的數字添加到剩余記錄的末尾。
試著舉個例子:
我有
A,B,C,1
A,B,C,2
A,B,C,3
D,E,F,1
D,E,F,3
我想要
A,B,C,"1,2,3"
D,E,F,"1,3"
在這一點上,我已經研究了幾個小時的解決方案,但據我所知,我幾乎一無所獲(我對腳本撰寫完全陌生),我可能需要在某些條件下使用for /f命令或findstr命令,但我真的很糾結從哪里開始。
uj5u.com熱心網友回復:
@ECHO OFF
setlocal enabledelayedexpansion
for /f "tokens=1-3,* delims=," %%a in (t.csv) do (
set "_%%a,%%b,%%c=!_%%a,%%b,%%c!%%d,"
)
(for /f "tokens=1,2 delims=_=" %%a in ('set _') do (
set "D=%%b
echo %%a,"!D:~0,-1!"
))>output.csv
第一個回圈收集資料(將第 4 列添加到名為“column1 to 3”
的變數中)。第二個回圈列印變數名稱(=“column 1 to 3”)加上收集的“columns 4”,同時洗掉最后一個逗號并添加引號并將整個回圈輸出重定向到一個檔案。
輸出樣本資料:
A,B,C,"1,2,3"
D,E,F,"1,3"
uj5u.com熱心網友回復:
@ECHO OFF
SETLOCAL
rem The following settings for the directories and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q74435774.txt"
SET "outfile=
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/533965.html
下一篇:檔案名批量包含字串
