批处理命令以查找带有换行符的替换字符串 [英] Batch command to find a replace string with line break
问题描述
我拥有的数据大多以一种可以将其转换并导入到电子表格中的方式进行组织.但是某些行有回车符和我当前的批处理文件将不使用的文本.
I have data that is mostly organized in a way that I can convert and import into a spreadsheet. But certain lines have carriage returns and text that my current batch file won't use.
良好数据:
Pipers Cove × 2 $25.00
Pipers Cove Petite × 2 $25.00
Pipers Cove Plus × 2 $25.00
Nordic Club × 2 $25.00
Whiteout × 1 $12.50
错误数据:
Pipers Cove Kids × 2
Size:
Large - ages 10 to 12
$20.00
Pipers Cove Kids × 2
Size:
Medium - ages 6 to 8
$20.00
Pipers Cove Kids × 2
Size:
Small - ages 2 to 4
$20.00
我需要删除以Size
,Small
,Medium
或Large
开头的2行,并让美元金额跟随数量编号,以便我的批处理文件可以将其转换为CSV文件,因此上.
I need to remove the 2 lines starting with Size
, Small
, Medium
, or Large
and have the dollar amount follow the quantity number so my batch file can convert it to a CSV file and so on.
推荐答案
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q40953616.txt"
SET "outfile=%destdir%\outfile.txt"
SET "part1="
(
FOR /f "usebackqdelims=" %%i IN ("%filename1%") DO (
ECHO %%i|FIND "$" >NUL
IF ERRORLEVEL 1 (
REM $ not found - set part1 on first such line
IF NOT DEFINED part1 SET "part1=%%i"
) ELSE (
REM $ found - see whether at start or not
FOR /f "tokens=1*delims=$" %%a IN ("%%i") DO (
IF "%%b"=="" (
REM at start - combine and output and reset part1
CALL ECHO %%part1%% %%i
SET "part1="
) ELSE (
ECHO %%i
)
)
)
)
)>"%outfile%"
GOTO :EOF
您需要更改sourcedir
和destdir
的设置以适合您的情况.
You would need to change the settings of sourcedir
and destdir
to suit your circumstances.
我使用了一个名为q40953616.txt
的文件,其中包含您的数据用于测试.
I used a file named q40953616.txt
containing your data for my testing.
产生定义为%outfile%
Produces the file defined as %outfile%
扫描文件的每一行.如果该行不包含$
,则将第一行这样的行保存在part1
中.
否则,标记该行.如果只有1个令牌,则$
在该行的开头,因此需要与part1
一起输出
否则,只需重新排线即可.
Scan each line of the file. If the line does not contain $
then save the first such line in part1
.
Otherwise, tokenise the line. If there is only 1 token, then the $
is at the start of the line, so it needs to be output combined with part1
Otherwise, just regurgitate the line.
这篇关于批处理命令以查找带有换行符的替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!