INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)

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 

RE: trying to replace part of a line with a variable found later in a 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close