[英]Text file string removal/replacement with Windows batch file
我想删除点后的三个数字,或者删除点后的所有内容.
和之前;
使用Windows批处理脚本。
之前
ABC; CDEF; GH; 123:456.XXX; EFG; 789:123.XXX
ABC; CDEF; GH; 123:456.XXX; EFG; 789:123.XXX
...
后
ABC; CDEF; GH; 123:456; EFG; 789:123
ABC; CDEF; GH; 123:456; EFG; 789:123
...
我一直在尝试使用myVar:~0,-4
但是在用其他字符串替换时我不知道如何使用它:
set "str=;"
for /f "tokens=* delims=." %%A in (%input%) do set myVar=%%A & echo !myVar:%myVar:~0,-4%=%str%! >> %output%
for /f "tokens=1-3 delims=." %%A in (%input%) do set "myVar1=%%A"&set "myVar2=%%B"&set "myVar3=%%C" & echo !myVar1!!myVar2:~3!!myVar3:~3! >> %output%
要么
for /f "tokens=1-3 delims=." %%A in (%input%) do set "myVar2=%%B"&set "myVar3=%%C" & echo %%A!myVar2:~3!!myVar3:~3! >> %output%
这是我的第一次尝试-假设您的结构正好包含2 .
,每个后跟3个要删除的字符。
使用您的示例,此方法不需要知道令牌计数。
@Echo Off
SetLocal EnableDelayedExpansion
Copy/Y "input.txt" "output.txt">Nul
(For /F "UseBackQDelims=" %%A In ("output.txt") Do (Set "OL="
For %%B In (%%A) Do Set "OL=!OL!;%%~nB"
Echo=!OL:~1!))>"input.txt"
::Del "output.txt"
Pause
如果您对input.txt
的内容感到满意,则可以从line 7
删除前两个字符::
。 如果没有,请不要担心什么都不会丢失,您可以删除input.txt
并重命名output.txt
为input.txt
。
我已经使用了input.txt
和output.txt
,请根据需要调整这两个名称
此方法适用于放置在任意位置的任意数量的点:
@echo off
setlocal EnableDelayedExpansion
rem Get file lines
(for /F "delims=" %%a in (input.txt) do (
rem Split line at dots
set "line=%%a"
set "myVar="
for %%b in ("!line:.=" "!") do (
if not defined myVar (
rem Copy first part
set "myVar=%%~b"
) else (
rem Eliminate first three chars from rest of parts
set "part=%%~b"
set "myVar=!myVar!!part:~3!"
)
)
echo !myVar!
)) > output.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.