好的,我还有另一个与CSV相关的问题。我会尽力解释这个问题。使用批处理文件,我需要根据CSV第一列中的值将值插入到csv的列中。例如,如果列A包含值“BCD”,则将“Value1”插入到列B中的相应行上。如果列A包含值“HIJ”,则将“Value2”插入到列B中的相应行上。以下是我要求的例子:
Input:
ABCDE OTHERDATA OTHERDATA
ABCDE OTHERDATA OTHERDATA
ABCDE OTHERDATA OTHERDATA
FGHIJ OTHERDATA OTHERDATA
FGHIJ OTHERDATA OTHERDATA
FGHIJ OTHERDATA OTHERDATA
Output:
ABCDE Value1 OTHERDATA OTHERDATA
ABCDE Value1 OTHERDATA OTHERDATA
ABCDE Value1 OTHERDATA OTHERDATA
FGHIJ Value2 OTHERDATA OTHERDATA
FGHIJ Value2 OTHERDATA OTHERDATA
FGHIJ Value2 OTHERDATA OTHERDATA
我希望我已经解释得这么好了!
答案 0 :(得分:0)
@ECHO OFF
SETLOCAL
(
FOR /f "tokens=1*" %%a IN (q22150040.txt) DO (
SET "insert="
FOR /f "tokens=1*" %%h IN (insert.txt) DO (
ECHO %%a|FINDSTR "%%h" >nul
IF NOT ERRORLEVEL 1 SET "insert=Y"&ECHO(%%a %%i %%b
)
IF NOT DEFINED insert (ECHO(%%a %%b)
)
)
)>newfile.txt
GOTO :EOF
我使用了一个名为q22150040.txt
的文件,其中包含我的测试数据。
Insert.txt
包含
BCD Value1
HIJ Value2
答案 1 :(得分:0)
@echo off
setlocal EnableDelayedExpansion
rem Define the replacement set
set n=0
for %%a in ("BCD=Value1"
"HIJ=Value2") do (
set /A n+=1
set "rep[!n!]=%%~a"
)
(for /F "tokens=1*" %%a in (input.txt) do (
set "col1=%%a"
set insert=NotFound
for /L %%i in (1,1,%n%) do for /F "tokens=1,2 delims==" %%x in ("!rep[%%i]!") do (
if "!col1:%%x=!" neq "%%a" set insert=%%y
)
echo %%a !insert! %%b
)) > output.txt
编辑:添加了输出示例
C:\> type input.txt
ABCDE OTHERDATA OTHERDATA
ABCDE OTHERDATA OTHERDATA
ABCDE OTHERDATA OTHERDATA
FGHIJ OTHERDATA OTHERDATA
FGHIJ OTHERDATA OTHERDATA
FGHIJ OTHERDATA OTHERDATA
C:\> test
C:\> type Output.txt
ABCDE Value1 OTHERDATA OTHERDATA
ABCDE Value1 OTHERDATA OTHERDATA
ABCDE Value1 OTHERDATA OTHERDATA
FGHIJ Value2 OTHERDATA OTHERDATA
FGHIJ Value2 OTHERDATA OTHERDATA
FGHIJ Value2 OTHERDATA OTHERDATA
答案 2 :(得分:0)
这使用名为repl.bat
的帮助程序批处理文件 - 从https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
将repl.bat
放在与批处理文件相同的文件夹中或放在路径上的文件夹中。
type "file.csv"|repl "(.*BCD.*?) " "$1 Value1 "|repl "(.*HIJ.*?) " "$1 Value2 " >"newfile.csv"