我有一个这样的文本文件:
N1 G17 G90
N3 G54
N5 S3000
N7 M03
N9 F2000
N11 G01 X0 Y0
N13 G01 X0 Y100
N15 G01 X100 Y100
N17 G01 X50 Y50
N19 G01 X30 Y30
N21 M05
N23 M30
我需要一个批处理文件,我可以在X或Y后为evry numer添加一个特定的数字 因此,如果我将数字200添加到X和100到Y,这将是文件
N1 G17 G90
N3 G54
N5 S3000
N7 M03
N9 F2000
N11 G01 X200 Y100
N13 G01 X200 Y200
N15 G01 X300 Y200
N17 G01 X250 Y150
N19 G01 X230 Y130
N21 M05
N23 M30
我似乎无法找到有关如何做到这一点的任何信息(我可能错误地搜索了......)
what if i got more tokens in my text? like this N1 G17 G90
N3 G54
N5 S24000
N7 M03
N9 G00 X2675.766 Y427.409 Z730
N11 G00 X2675.766 Y427.409 Z730
N13 G00 X2675.766 Y427.409 Z730
N15 G00 X2675.766 Y427.409 Z505
N17 F4000
N19 G01 X2675.766 Y427.409 Z447.5
N21 F4000
N23 G01 X2565.966 Y475.823 Z447.5
N25 F4000
N27 G02 X1852.832 Y871.38 Z447.5 I4373.42 J4575.032
N29 G03 X705.065 Y871.38 Z447.5 I1278.948 J28.138
N31 G02 X-8.069 Y475.823 Z447.5 I-1815.523 J4575.032
N33 M05
N35 M30
我试过这样但是那个dident工作
@echo off
setlocal EnableDelayedExpansion
(for /F "tokens=1-7" %%a in (fr17149.nc) do (
if "%%d" equ "" (
echo %%a %%b %%c
) else (
for /F "tokens=1,2 delims=XY " %%C in ("%%c %%d") do set /A X=%%C+200, Y=%%D+100
echo %%a %%b X!X! Y!Y! %%e %%f %%g
)
)) > output.txt
答案 0 :(得分:2)
@echo off
setlocal EnableDelayedExpansion
(for /F "tokens=1-4" %%v in (input.txt) do (
if "%%y" equ "" (
echo %%v %%w %%x
) else (
for /F "tokens=1,2 delims=XY " %%X in ("%%x %%y") do set /A X=%%X+200, Y=%%Y+100
echo %%v %%w X!X! Y!Y!
)
)) > output.txt
答案 1 :(得分:1)
为了证明其他语言更适合这种文本操作,下面的awk脚本只用了五行就完成了这项工作:
/ X[0-9]+ Y[0-9]/ {
$3 = "X" substr($3,2) + 200
$4 = "Y" substr($4,2) + 100
}
{print}
使用命令awk -f myscript.awk input.txt >output.txt
运行它。