Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mirror A axis in NC File 2

Status
Not open for further replies.

ebw

Technical User
Aug 22, 2002
2
US
I have a file as follows

PARTNO TEST
%
O251(TEST)
G1X-.0184Y-.8618Z.7723A355.0F10
G8Y-.8452Z.7708A.0
X-.0185Y-.8284Z.7694A5.0
X-.0187Y-.8115Z.7679A10.0
X-.0188Y-.7945Z.7664A15.0
X-.0189Y-.7773Z.7649A20.0
X-.019Y-.7601Z.7634A25.0
X-.0185Y-.8284Z.7694A-5.0
X-.0187Y-.8115Z.7679A-10.0
X-.0188Y-.7945Z.7664A-15.0
X-.0189Y-.7773Z.7649A-20.0
X-.019Y-.7601Z.7634A-25.0
G28
M2

I need to change any number immediately following an A by subtracting its absolute value from 360 and changing its sign. Any line which does not contain an A followed by a number should be pass through unchanged. The output should be:

PARTNO TEST
%
O251(TEST)
G1X-.0184Y-.8618Z.7723A-5.0F10
G8Y-.8452Z.7708A.0
X-.0185Y-.8284Z.7694A-355.0
X-.0187Y-.8115Z.7679A-350.0
X-.0188Y-.7945Z.7664A-345.0
X-.0189Y-.7773Z.7649A-340.0
X-.019Y-.7601Z.7634A-335.0
X-.0185Y-.8284Z.7694A355.0
X-.0187Y-.8115Z.7679A350.0
X-.0188Y-.7945Z.7664A345.0
X-.0189Y-.7773Z.7649A340.0
X-.019Y-.7601Z.7634A335.0
G28
M2

Note that if you convert the output file with the same program you should get back to the input file.

Thanks in advance.

Ernie
 
Code:
# If no "A", print line and read next line.
!/A/ { print; next }

{ shatter( $0, array, "[A-Z]" )
  for (i=2; i in array; i+=2 )
    if ( "A" == array[i] )
    { num = array[i+1]
      newnum = 360.0 - abs(num)
      if (num >= 0) newnum = -newnum
      array[i+1] = newnum
    }
  for (i=1; i in array; i++)
    printf "%s", array[i]
  print ""
}

function abs( x )
{ if ( x < 0 )
    x = -x
  return x
}

# Produces array of nonmatching and matching
# substrings. The size of the array will
# always be an odd number. The first and the
# last item will always be nonmatching.
function shatter( s, array, re )
{ gsub( re, "\1&\1", s  )
  return split( s, array, "\1" )
}

Save as [tt]mirror-a.awk[/tt] and run with
[tt]awk -f mirror-a.awk oldfile >newfile[/tt]

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.
 
A starting point:
nawk '{
if(match($0,/A[\-0-9\.]+/)){
o=substr($0,RSTART+1,RLENGTH-1)
n=(o<0?360+o:eek:-360)
printf "%s%0.1f%s\n",substr($0,1,RSTART),n,substr($0,RSTART+RLENGTH)
}else print
}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks a lot guys!

I used PHV's program with slight modifications to take into account a couple of things that I forgot to tell you.

Thanks again

Ernie
 
Now faster and smaller.
Code:
{ shatter( $0, array, "A[-0-9.]+" )
  for (i=1; i in array; i++ )
  { if (i%2==0)
    { num = substr( array[i], 2 )
      newnum = (num < 0 ? 360 + num : num - 360)
      array[i] = "A" sprintf("%.1f", newnum)
    }
    printf "%s", array[i]
  }
  print ""
}

function shatter( s, array, re )
{ gsub( re, "\1&\1", s  )
  return split( s, array, "\1" )
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top