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
Kristof.V
  • 143
  • 1
  • 7

2 Answers2

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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • THANKS!!!! I don't understand what all that does, but it seems to work like a charm :) – Kristof.V Feb 09 '17 at 23:24
  • 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](http://stackoverflow.com/a/42149125/638028) a shorter script using [awk](http://gnuwin32.sourceforge.net/packages/gawk.htm). 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. – Klitos Kyriacou Feb 10 '17 at 00:01
  • 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... – Kristof.V Feb 10 '17 at 11:25
  • @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: – Aacini Feb 10 '17 at 15:04
  • 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... – Aacini Feb 10 '17 at 15:05
  • I have experience with batch and can understand exactly how your batch file works, but only after doing `FOR /?` to remind myself of the intricacies of the `FOR /F` option. Also, I believe that batch can be a highly expressive language and can be just as good (sometimes I believe better) than bash or other languages that are common on *nix. Many people install Cygwin just to use bash because they believe it's much more powerful than batch. I usually don't bother, and just write my scripts in batch. What I'm saying here is that batch is not the best for text manipulation. – Klitos Kyriacou Feb 10 '17 at 15:30
  • @Aacini if i got 7 tokens "N27(1) G02(2) X1852.832(3) Y871.38(4) Z447.5(5) I4373.42(6) J4575.032(7)" how can i make it work then? i've tried to add more tokens but it doesent seem to work (I edited the question for more info about how my code is and the text file) – Kristof.V Feb 11 '17 at 14:11
  • You must note that your _new specifications_ describe an **entirely different problem!** You _always_ must describe the _real_ problem and not expect that a given solution be modified several times through several small changes up to fulfill your requirements. Besides, Batch files can only manage integer numbers, so your _new request_ require a special treatment. I suggest you to post a new question with _complete requirements_. If you want a fast answer from me, post a comment here after you posted the new question... – Aacini Feb 11 '17 at 15:13
  • @Aacini thaks for the reply, heres my other post: http://stackoverflow.com/questions/42178403/edit-part-of-text-in-text-file-using-batch – Kristof.V Feb 11 '17 at 16:39
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.

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70