Hi Perl Gurus! For someone more seasoned in perl coding I'm sure this is trivial, but I am a complete noob.
I have a text file that requires post-processing to remove lines matching certain control command lines, as well as pre-pend characters and remove characters from other lines.
My existing script works to remove lines that match certain control commands- G10, G11, G92, M190, but I am finding it very hard to figure out how to search/replace characters within each line without overwriting the whole line.
The two strings I need to search and replace on a line by line basis are:
1) search for G1 ZNN.NNN where N is a number and . is a decimal, and replace with G1 Z-NN.NNN, retaining remainder of text on line
2) search for EN.NNNNN where N is a number and . is a decimal, and replace with null, retaining remainder of text on line.
My existing code is:
#!/usr/bin/perl -i.before_postproc
# Author : David Sherwood
# Version : 1.0
# Copyright : none.
#
# Slice3R GCODE laser cutter post-processor
use strict;
use warnings;
# read stdin and any/all files passed as parameters one line at a time
while (<> ) {
if (/G11/) {
# if we have an un-retraction line, replace it with laser powe
+r on
print "M400 ; wait for moves to finish\nM104 S100 ; laser on\n
+";
}
elsif (/G10/) {
# if we found a retraction line, replace it with laser power o
+ff
print "M104 S0 ; laser off\n";
}
elsif (/G92/) {
# if we found an extruder reset command line, remove it
print "";
}
elsif (/M190/) {
# if we found a heat bed command line, remove it
print "";
}
else {
print or die $!;
}
}
Sample Data:
G92 E0 ; want to remove all G92 lines (works now)
T1
G1 Z0.050 F7800.000
G10 ; retract
G92 E0
G1 Z0.150 F7800.000
G1 X14.725 Y-8.975 F7800.000
G1 Z0.050 F7800.000
G11 ; unretract
G92 E0
G1 X14.725 Y8.975 E0.01465 F120.000
G10 ; retract
G92 E0
G1 Z0.150 F7800.000
G1 X-9.585 Y-0.615 F7800.000
G1 Z0.050 F7800.000
G11 ; unretract
G92 E0
Expected Data
T1
G1 Z-0.050 F7800.000
M104 S0 ; laser off
G1 Z-0.150 F7800.000
G1 X14.725 Y-8.975 F7800.000
G1 Z-0.050 F7800.000
M400 ; wait for moves to finish
M104 S100 ; laser on
G1 X14.725 Y8.975 F120.000
M104 S0 ; laser off
G1 Z-0.150 F7800.000
G1 X-9.585 Y-0.615 F7800.000
G1 Z-0.050 F7800.000
M400 ; wait for moves to finish
M104 S100 ; laser on
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.