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

Updating the first row (header) in a file.

Status
Not open for further replies.

mackers43

Programmer
Dec 17, 2010
1
IE
Hi all

Hoping you can help me.

I need to write some Expect TCL that will open a file (RSFO0004) and overwrite the characters on the right side of the first row with the characters stored in a variable ($sIDCurValue). The character length in the variable can vary in size from 2-6 characters long.

An example is below:

$sIDCurValue = 1320

Original file values =
HROS003201012080000000065
TROS0032010120800000000650000000000

New file values =
HROS003201012080000001320
TROS0032010120800000000650000000000

My current code for this scenario is below but it is deleting the entire first row. Any help would be much appreciated:

exp_send "cp RSFO0004 RSFO0004.temp\n"
expect -re $prompt

set fTempRSFO0004 [open "RSFO0004.temp" r]
set fRSFO0004 [open "RSFO0004" w]

set sLineNum 0
while {[gets $fTempRSFO0004 sRSOLine] >= 0} {
if {$sLineNum == 0} {
set sRSOLine [string replace $sRSOLine [expr [string length $sRSOLine] - [string length $sIDCurValue]] [string length $sRSOLine] $sIDCurValue]
}
puts $fRSFO0004 $sRSOLine
}

close $fTempRSFO0004
close $fRSFO0004
 
I think one problem is that you never increment "sLineNum". You continue reading: [gets $fTempRSFO0004 sRSOLine], which sets sRSOLine, and since $sLineNum continues to be 0, you continue replacing.

The replacement syntax is correct:
Code:
% set s HROS003201012080000000065
HROS003201012080000000065
% set snew 1320
1320
% string replace $s [expr {[string length $s]-[string length $snew]}] [string length $s] $snew
HROS003201012080000001320
%
so I'm guessing either sRSOLine or sIDCurValue or both are not what you think they are.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top