0

I've got a text file like this:

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

i need a batch file where i can add a specific number to evry numer aftehr X or Y so if i add the number 200 to X and 100 to Y this would be the file

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

i can't seem to find any information about how to do that(i probably searching wrong or so..)

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

i tried it like this but that dident work

@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

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

9 Comments

THANKS!!!! I don't understand what all that does, but it seems to work like a charm :)
I must say I am impressed that you've managed to get a batch file to successfully do this. As you've challenged me in a comment to a now-deleted answer, I've added a shorter script using awk. This was much easier and took less effort to write than your batch script. And it is easier to read and understand. So I think awk wins the language war but you win the skills war.
if i got a file with a line like this, N27 G02 X1852.832 Y871.38 Z447.5 I4373.42 J4575.032 how many tokens do i need then? since thats the most parts i can get in a line. only need to add a number to X and Y the other numbers just need to be echo'd like they are. and since my numbers can be seperated by points "." i think it thinks its a new token.. but there aren't always points...
@KlitosKyriacou: You are the first person that posted a reply after my challenge. Although this may prove that awk is "better suited" to this kind of text manipulation, the point about be an "easier solution" depends on the need to install awk in the computers that will use it and how easy the script can be later modified or a problem with it can be solved by the OP. Just compare the number of questions in this site on awk vs. batch-file. However, my point about your comments on Batch files remains:
If you review my Batch solution you'll realize that is a simple program with a couple for commands and one if, so your comment about "be impressed" by this solution just prove that you don't know Batch files enough. I can't express an educated opinion about an awk solution if I don't know awk capabilities at all. However, there are a lot of people that post comments on "Batch is difficult, is easier to use any other language". IMHO, those people should not express such comments if they don't know Batch files enough...
|
1

Just to prove that other languages are better suited to this kind of text manipulation, the following awk script does the job in just five short lines:

/ X[0-9]+ Y[0-9]/ {
    $3 = "X" substr($3,2) + 200
    $4 = "Y" substr($4,2) + 100
}
{print}

Run it with the command awk -f myscript.awk input.txt >output.txt.

1 Comment

Need to take alook at awk scripts then and how all that works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.