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. =/

  • Oh I see... Thank you. I will edit my question then. – 71GA Feb 6 '15 at 9:58
  • sed 's/\([0-9]\{4\}\)[0-9]*/\1/g' – Costas Feb 6 '15 at 10:00
  • awk -F'Y' -v OFS='Y' '{print substr($1,1,5), substr($2,1,4)}' – Costas Feb 6 '15 at 10:15
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.

  • 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 6 '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 6 '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 6 '15 at 10:38
  • Or even more close '/^X/s/\([XY].\{4\}\)[^Y]\{1,2\}/\1/g' – Costas Feb 6 '15 at 10:47
  • 1
    @71GA Try sed 's/\(^X[[:digit:]]*\)[[:digit:]]\(Y[[:digit:]]*\)[[:digit:]]/\1\2/' – jimmij Feb 6 '15 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.