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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to replace part of a line with a variable found later in a file

Status
Not open for further replies.

aluminum2

Technical User
May 17, 2013
1
US

I am trying to write a tcl script that finds information in a later line in the file and then replaces part of a line found in a earlier line. I was able to find the line with the information and write it as a variable $g71newline. I also want this done in the while loop because this code could be repeated.


IN the following file the code will find the line with N150 LRCCEL.150
and take the number 150 after the "." then the variable $g71newline will subtract 10 from it and write it.

Thats the part I got. What I want to do next is find part of the line that contains QLRCCE and replace it with $g71newline so that the line reads

N70 G71 P80 Q140 FR0.0050 UR0.0010 instead of
N70 G71 P80 QLRCCE FR0.0050 UR0.0010



Code:
N10 G40 G18 G700 G90
N20 ;Operation : FINISH_TURN_OD_COPY
N30 DIAMON
N40 T2 M06
N50 G54
N60 G71 U0.1010 R0.0990
N70 G71 P80 QLRCCE FR0.0050 UR0.0010
N80 G96 S300 M04
N90 G00 X1.0158 Z.4985 D00
N100 G01 X1.047 F.03
N110 X1.2488 F.005
N120 G03 X1.3147 Z.4686 I0.0 K-.0331
N130 G01 X1.3148 Z.4679
N140 X1.315 Z-.0165
N150 LRCCEL.150
N160 X1.3462 F.03
N170 G00 Z5.
N180 X7.
N190 M30




here is the code I have so far. anything with MOM_ in front of it is a pre-defined variable in the program I am running it in.


Code:
global mystring
global splitname
global splitname1
global splitlrce
global searchWord
global replaceword
global g71newline
global g71endline
global g71endseqno
global mom_output_file_full_name
global OLD_File
global NEW_File
global OLD_File2
global NEW_File2
global mom_def_sequence_increment
global mom_command_status




MOM_close_output_file $mom_output_file_full_name
set OLD_File [open $mom_output_file_full_name r+]
set NEW_File [open ${mom_output_file_full_name}_new w+]

while {[gets $OLD_File line] >= 0} {



    if {[string match "*LRCCEL*" $line]} {

set g71endline [lindex [split $line .] 1]
set g71endseqno [expr $g71endline - $mom_def_sequence_increment  ]
    set g71newline "N[format "%.0f" $g71endseqno]"
    # replace the line
    puts $NEW_File $g71newline


#hopefully add some sort of foreach or another while loop here. I just didn't have any luck trying anything. 

    } else {
    # otherwise, just copy the line
    puts $NEW_File $line
    }
}

close $OLD_File
close $NEW_File
 
Unless your file is so huge you can't (>500MB?) I would read the entire file into a list.
Code:
set lstIN [split [read OLD_FILE] \n]

Then you can keep track of the indices of the lines you want to change and change them. Then you can write the new/modified list out when you're done.
Code:
foreach str $lstIN {puts $NEW_FILE $str}

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top