0

I have create a gerber file and it has lines inside which look like this:

X90095Y15350
X100095Y15350
X110095Y15350
X120095Y15350
X120095Y25350
X110095Y25350
X100095Y25350
X90095Y25350
X80095Y25350
X160095Y25350

These are coordinates of the drill path - mind the X and Y. Number after the X and Y varies - it is sometimes a 6-digit and sometimes 5-digit.


Now I need a script which would first identify if the line starts with an X and then use sed or any other Linux tool to convert this into a format where X and Y are both 4-digits long so that line:

X160095Y25350

becomes:

X1600Y2535

Can anyone help me. I am a bit too weak in regular expressions in order to solve this myself... If there is any other command which would do this it would allso be fine.


ADD

The anwser provided is working OK and I could use it to reformat my file. After uploading it into my application I noticed that we did a wrong conversion.

I am sorry for this, but now I know what needs to be done. So If a line starts with an "X" we only have to remove the last digit of numbers after X and Y. For example if I have:

X90095Y15350
X100095Y15350
X110095Y15350

I need to get the last digit of every position (X and Y) removed:

X9009Y1535
X10009Y1535
X11009Y1535

Could someone help me with this one. =/

71GA
  • 1,106

1 Answers1

1

Try this:

sed 's/\(^X[[:digit:]]\{4\}\)[[:digit:]]*\(Y[[:digit:]]\{4\}\)[[:digit:]]*/\1\2/'

If line starts with X (^X) then all digits which are not inside parenthesis \(\) are deleted.

jimmij
  • 47,140
  • Do you think it is not much over? If you wants to check your can do it before substitution: /^X[0-9]*Y[0-9]*$/s ... – Costas Feb 06 '15 at 10:20
  • @Costas I'm not sure what you are trying to say. Number after the X and Y varies - it is sometimes a 6-digit and sometimes 5-digit and first identify if the line starts with an X define the algorithm as I see it. – jimmij Feb 06 '15 at 10:29
  • sed '/^X/s/\([0-9]\{4\}\)[0-9]*/\1/g' that is what I am told. Even you wants to include XY much simple '/^X/s/\([XY][0-9]\{4\}\)[0-9]*/\1/g' – Costas Feb 06 '15 at 10:38
  • Or even more close '/^X/s/\([XY].\{4\}\)[^Y]\{1,2\}/\1/g' – Costas Feb 06 '15 at 10:47
  • @Costas It is too simplified to my taste. For the line XY90095Y25350 the '/^X/s/\([XY][0-9]\{4\}\)[0-9]*/\1/g' will return XY9009Y2535 and '/^X/s/\([XY].\{4\}\)[^Y]\{1,2\}/\1/g' will return XY900Y2535. Both seems wrong to me. – jimmij Feb 06 '15 at 10:51
  • If we told about extra check that easy to do by exchanging /^X/ to /^X[[:digit:]]/ and if you need you can do it stronger and stronger up to same as in my first comment. – Costas Feb 06 '15 at 10:56
  • This anwser does what it is claimed to do. But I noticed now, that I need something else - take a look at the ADD. – 71GA Feb 06 '15 at 13:23
  • 1
    @71GA Try sed 's/\(^X[[:digit:]]*\)[[:digit:]]\(Y[[:digit:]]*\)[[:digit:]]/\1\2/' – jimmij Feb 06 '15 at 13:27
  • It works like a charm. Now please help me to understand sed. Is there any sed online trainer where i can input sed commands and it will show me what it does. I found a nice one for Reg. Exp. here: http://www.regexr.com/ – 71GA Feb 06 '15 at 13:46
  • @71GA You may find many suggestions about sed tutorials here: http://unix.stackexchange.com/questions/2434/is-there-a-basic-tutorial-for-grep-awk-and-sed. Personally I use just info sed... And to test regex for example you can use https://www.regex101.com/ – jimmij Feb 06 '15 at 14:01