我有一个包含18个字段的csv文件。第四个字段包含几个我想删除的子字符串,包括(C:1-0-1),(NET)和(MR)。如果子字符串包含括号,则以下内容不起作用。如何使此批处理文件使用通配符(C *)删除任何子字符串?我使用“ rem”来帮助进行故障排除,但是最终的批处理文件需要从第四个字段中删除(C *),(NET)和(MR)。
1,"MAY090178","D ","BATMAN HUSH COMPLETE TP (C: 1-1-0)",24.99,11.2455,11.25,3,7, ,62464962,"76194127923700111","1401223176","978140122317552499"," ",0,"DC COMICS",000000
1,"NOV141747","F ","BLACK BUTLER GN VOL 19 (C: 1-1-0)",13.00,7.5400,7.54,3,7, ,62464962,"","0316259403","978031625940851300"," ",0,"YEN PRESS",127143
2,"MAY151682","F ","BLACK BUTLER GN VOL 20 (C: 1-1-0)",13.00,7.5400,15.08,3,7, ,62464962,"","0316305014","978031630501351300"," ",0,"YEN PRESS",127143
1,"AUG180208","D ","BULLY WARS #2 CVR A CONLEY",3.99,1.7955,1.80,1,7, ,62464962,"70985302666200211","",""," ",0,"IMAGE COMICS",140749
1,"OCT180162","D ","BULLY WARS #4 CVR A CONLEY",3.99,1.7955,1.80,1,7, ,62464962,"70985302666200411","",""," ",0,"IMAGE COMICS",140749
10,"SUPPLY123","J ","COMICARE LONG COMIC BOX (SOLD IN 10) (NET)",2.53,2.5330,25.33,12,7, ,62464962,"603259601235","",""," ",0,"",000000
rem | Remove (C: 1-1-0) etc from titles
for /f "tokens=1,* delims=¶" %%A in ('"type datatest_update.csv"') do (
SET string=%%A
rem SET string2=%%A
rem SET string3=%%A
setlocal EnableDelayedExpansion
SET modified=!string:(C: 1-1-0) = !
rem SET modified2=!string:EXTRAS (NET) = !
rem SET modified3=!string:(MR) = !
>> datatest_update_2.csv echo(!modified!
endlocal
)
编辑:我已将文件更新为以下文件,但未运行。我应该使用delims =而不是delims =¶吗?
rem | Remove (C: 1-1-0) etc from titles
for /f "tokens=1,* delims=¶" %%A in ('"type datatest_update_6.csv"') do (
SET string=%%A
SET modified=!string: (C: 1-1-0)=!
SET modified=!modified: (NET)=!
SET modified=!modified: (MR)=!
setlocal EnableDelayedExpansion
>> datatest_update_7.csv echo(!modified!
endlocal
)
答案 0 :(得分:1)
这可以通过基本的替换功能来完成。我们要做的第一件事是搜索包含(C: 1-1-0)
EX:"BATMAN HUSH COMPLETE TP (C: 1-1-0)"
的数据。从这里我们可以将其设置为该行中的"Replace this"
。 "search this"
字符串将是整个Raw
行数据。
由于您的数据在第四栏中,因此我们可以使用delims=,
来帮助收集这些数据。在loop
中找到此数据后,可以使用CALL
命令调用replace函数。替换功能将仅编辑所需的行,以便保留其他未修改的数据。
@ECHO OFF
@setlocal enabledelayedexpansion
Set "InputFile=File.csv"
Set "FindText=(C: 1-1-0)"
Rem | Expand Document To Loop
for /f "tokens=*" %%A in ('Type "%InputFile%"') do (
Set "OriginalLine=%%A"
Rem | Only Find The Parses Containing "(C:"
for /f "tokens=1,2,3,4* delims=," %%B in ('Echo %%A^| find /I "%FindText%"') do (
Rem | Remove The Proper String
SET "RemoveMe=%%E"
CALL :EditLine !RemoveMe!
)
)
GOTO :EOF
:EditLine
REM | Change The %RemoveMe% To ""
Set ReplaceText=!OriginalLine:%1=""%!
REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %InputFile%') DO (
SET line=%%n
SET Modified=!line:%OriginalLine%=%ReplaceText%!
SET Modified=!Modified:*:=!
REM | Output the entire edited INI to a temporary file.
>> %InputFile%.TEMP ECHO(!Modified!
)
Rem | Delete Original File, Restore New
DEL %InputFile%
REN %InputFile%.TEMP %InputFile%
GOTO :EOF
编辑:
基于一个误解,我虽然希望完全删除包含(C: 1-0-1)
的列的全部内容。该脚本将解决此问题和“通配符”问题。
据我所知,find
命令没有批量扩展的通配符。如果我对此人有误,请在评论中纠正我。
但是,要克服此问题,我们可以简单地在包含(C:
的文档中查找行,然后在replace函数中删除第4列字符串的最后11个字符。使用find语句,您还可以将结果限制为以下示例:(C: 1-0
而不是(C:
-文件中的所有{{1 }}将被更改。
(C: 1-2
版本2-选择要编辑的字符串:
在此版本中,我将允许通过允许多个搜索字符串来选择要编辑的单独行。这也适用于“通配符”。
示例:1-0-*
函数将从文档的第四列中删除以下内容。
@ECHO OFF
@setlocal enabledelayedexpansion
Set "InputFile=File.csv"
Set "FindText=(C:"
Rem | Expand Document To Loop
for /f "tokens=*" %%A in ('Type "%InputFile%"') do (
Set "OriginalLine=%%A"
Rem | Only Find The Parses Containing "(C:"
for /f "tokens=1,2,3,4* delims=," %%B in ('Echo %%A^| find /I "%FindText%"') do (
Rem | Remove The Proper String
SET "RemoveMe=%%E"
CALL :EditLine !RemoveMe!
)
)
GOTO :EOF
:EditLine
REM | Remove The %FindText% To Restore Later
REM | Since the find command has no wild cards we will remove it via "~0,-11%"
REM | This will remove the last 11 characters in the string
Set SaveMe=!RemoveMe:"=%!
Set "SaveMe=%SaveMe:~0,-11%"
REM | Change The %RemoveMe% To %SaveMe%
Set ReplaceText=!OriginalLine:%1="%SaveMe%"%!
REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %InputFile%') DO (
SET line=%%n
SET Modified=!line:%OriginalLine%=%ReplaceText%%!
SET Modified=!Modified:*:=!
REM | Output the entire edited INI to a temporary file.
>> %InputFile%.TEMP ECHO(!Modified!
)
Rem | Delete Original File, Restore New
DEL %InputFile%
REN %InputFile%.TEMP %InputFile%
GOTO :EOF
-从现有列中删除。Call :FindText "(C: 1-1-0)" "(C: 1-1-2)" "(C: 1-2"
-从现有列中删除。(C: 1-1-0)
-从现有列中删除 ALL (C: 1-1-2)
。批处理脚本:
(C: 1-2
要获取有关任何命令的帮助,请执行以下操作:
1-2-*
@ECHO OFF
@setlocal enabledelayedexpansion
Rem | Configuration
Rem | To use the command, Call :FindText "Desired Text One" "Desired Text Two" "exc..."
Set "InputFile=File.csv"
Call :FindText "(C: 1-1-0)" "(C: 1-1-2)" "(C: 1-1-3)"
pause
GOTO :EOF
Rem | Loop Through Each Search Results
:FindText
for %%A in (%*) do (
Rem | Expand Document To Loop
for /f "tokens=*" %%B in ('Type "%InputFile%"') do (
Set "OriginalLine=%%B"
Rem | Only Find The Parses Containing "(C:"
for /f "tokens=1,2,3,4* delims=," %%C in ('Echo %%B^| find /I %%A') do (
Rem | Remove The Proper String
SET "RemoveMe=%%F"
CALL :EditLine !RemoveMe!
)
)
)
GOTO :EOF
:EditLine
REM | Remove The %FindText% To Restore Later
REM | Since the find command has no wild cards we will remove it via "~0,-11%"
REM | This will remove the last 11 characters in the string
Set SaveMe=!RemoveMe:"=%!
Set "SaveMe=%SaveMe:~0,-11%"
REM | Change The %RemoveMe% To %SaveMe%
Set ReplaceText=!OriginalLine:%1="%SaveMe%"%!
REM | Make sure we only edit the ListOfItems line.
FOR /F "delims=" %%n IN ('findstr /n "^" %InputFile%') DO (
SET line=%%n
SET Modified=!line:%OriginalLine%=%ReplaceText%%!
SET Modified=!Modified:*:=!
REM | Output the entire edited INI to a temporary file.
>> %InputFile%.TEMP ECHO(!Modified!
)
Rem | Delete Original File, Restore New
DEL %InputFile%
REN %InputFile%.TEMP %InputFile%
GOTO :EOF
call /?
set /?
for /?