To search and replace word into a text file from PB7 Script

Please read this first.
http://www.experts-exchange.com/Programming/Programming_Languages/PowerBuilder/Q_21848080.html

After adding the line the file looks like this.

kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover
AACIL,12 05 2006,23.6,24.3,23.3,23.55,1077500
AASM,12 05 2006,55,59.85,55,59.85,1500
ABCF,12 05 2006,9.25,9.6,9.25,9.45,273500
ABL,12 05 2006,105.75,109,105.75,107.15,469500
ABOT,12 05 2006,170,170,165.25,166.5,3600
ACBL,12 05 2006,90,91.4,89.55,89.7,955500
ACF,12 05 2006,16.45,16.9,16.3,16.9,410000
ACPL,12 05 2006,98,104,98,102.5,6100
ADTM,12 05 2006,3.25,3.25,3.25,3.25,2000


Now I want to replace "12 05 2006" with "2006/05/12" in the entire file.
Please use variable in the code. for example
ls_old_value =  "12 05 2006"
ls_new_value= "2006/05/12"


MateenAsked:
Who is Participating?
 
sandeep_patelCommented:
Hi,

Please check this code...

integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp

long start_pos=1
string ls_old_value, ls_new_value , ls_mystring

ls_old_value =  "12 05 2006"
ls_new_value= "2006/05/12"

ls_filename = 'c:\temp\test.txt'            // original file
ls_temp = 'c:\temp\test_copy.txt'      // Temporary file

li_filenum = FileOpen(ls_filename,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_temp,LineMode!,Write!,LockWrite!,Replace!)

Do While FileRead(li_filenum,ls_line) > 0
      ls_mystring = ls_line
      // Find the first occurrence of old_str.
      start_pos = Pos(ls_mystring, ls_old_value, start_pos)
      // Only enter the loop if you find old_str.
      DO WHILE start_pos > 0
             // Replace old_str with new_str.
             ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
      
             // Find the next occurrence of old_str.
             start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value))
      LOOP

      ls_line = ls_mystring
   FileWrite(li_filenum2,ls_line)
Loop

FileClose(li_filenum)
FileClose(li_filenum2)

li_filenum = FileOpen(ls_temp,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_filename,LineMode!,Write!,LockWrite!,Replace!)

Do While FileRead(li_filenum,ls_line) > 0
   FileWrite(li_filenum2,ls_line)
Loop

FileClose(li_filenum)
FileClose(li_filenum2)

LOGIC :
1) open original file in line by line read mode and temporary file to write in it.
2) read from original file line by line and check for all occurance of old value in it and replace with new value.
3) write new modified line to temporary file
4) close both files.
5) now again open original file in write mode with replace mode and temporary file in read mode
6) read each line from temporary file and write in original file
7) close both files.

I have not checked the code... let me know if any error or misbehave is there...

Regards,
Sandeep


0
 
sandeep_patelCommented:
Hi,

one more thing to add if there is no chance to repeat the date in a line you can remove do while loop.

Do While FileRead(li_filenum,ls_line) > 0
     ls_mystring = ls_line
     // Find the first occurrence of old_str.
     start_pos = Pos(ls_mystring, ls_old_value, start_pos)
     // Replace old_str with new_str.
     ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
     ls_line = ls_mystring
   FileWrite(li_filenum2,ls_line)
Loop

Regards,
Sandeep
0
 
MateenAuthor Commented:
Hi Sandeep,

Thanks for replying on Sunday.

I am sure your code will work. I would try it out on Monday.

Right now, I am closing this question.

If I face any problem , I would ask here.

You people are lovely. Help others for nothing.

Best Regards

Mateen

You may , please, have a look at
http://www.experts-exchange.com/Programming/Programming_Languages/PowerBuilder/Q_21849882.html
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
MateenAuthor Commented:
Hi sandeep

I have tested your code today.

Nothing happened.
0
 
sandeep_patelCommented:
Hi,

what you mean by nothing happened ??? It worked or not ? :(

And for your previous question why don't you add that logic in this logic itself. No need to open and write the same file again and again.

li_filenum = FileOpen(ls_filename,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_temp,LineMode!,Write!,LockWrite!,Replace!)

//// Add header line code ////
FileWrite(li_temp, "kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover")

DO while loop....

...

and rest of the logic

Regards,
Sandeep
0
 
MateenAuthor Commented:
Hi Sandeep
No replacement occured.

I tried

/*
LOGIC : Sandeep Patel
1) open original file in line by line read mode and temporary file to write in it.
2) read from original file line by line and check for all occurance of old value in it and replace with new value.
3) write new modified line to temporary file
4) close both files.
5) now again open original file in write mode with replace mode and temporary file in read mode
6) read each line from temporary file and write in original file
7) close both files.
*/

integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp

long start_pos=1
string ls_old_value, ls_new_value , ls_mystring

ls_old_value =  "12 05 2006"
ls_new_value= "2006/05/12"

ls_filename = 'd:\\download\\120052006.txt'    // original file
ls_temp     = 'c:\\temp\\test_copy.txt'        // Temporary file

li_filenum = FileOpen(ls_filename,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_temp,LineMode!,Write!,LockWrite!,Replace!)

Do While FileRead(li_filenum,ls_line) > 0
     ls_mystring = ls_line
     // Find the first occurrence of old_str.
     start_pos = Pos(ls_mystring, ls_old_value, start_pos)
     // Only enter the loop if you find old_str.
     DO WHILE start_pos > 0
           // Replace old_str with new_str.
           ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
     
           // Find the next occurrence of old_str.
           start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value))
     LOOP

     ls_line = ls_mystring
   FileWrite(li_filenum2,ls_line)
Loop

FileClose(li_filenum)
FileClose(li_filenum2)

li_filenum = FileOpen(ls_temp,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_filename,LineMode!,Write!,LockWrite!,Replace!)

Do While FileRead(li_filenum,ls_line) > 0
   FileWrite(li_filenum2,ls_line)
Loop

FileClose(li_filenum)
FileClose(li_filenum2)
//===========================


<<And for your previous question why don't you add that logic in this logic itself. No need to open and write the same file again and again.>>

Once all thing done, I would convert into one.

0
 
sandeep_patelCommented:
Hi,

first check that is it creating a temp file at c:\\temp\\test_copy.txt. If not it means you don't have rights to write at c:\\temp or may be temp folder not exist at c: drive. If it is creating file then put message boxes in do while loop and check the value of variable ls_mystring before replace and after replace

Do While FileRead(li_filenum,ls_line) > 0
     ls_mystring = ls_line

     messagebox('before', ls_mystring)

     // Find the first occurrence of old_str.
     start_pos = Pos(ls_mystring, ls_old_value, start_pos)
     // Replace old_str with new_str.
     ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)

     messagebox('after',ls_mystring)

     ls_line = ls_mystring
   FileWrite(li_filenum2,ls_line)
Loop

Hope it will help you to debug the code.

Regards,
Sandeep
0
 
MateenAuthor Commented:
Hi sandeep,

I tried my level best to make it work myself, but could not succeed.
However I have found the problem area.

  messagebox('ls_mystring_before',ls_mystring)    // your debug line
     
        // Find the first occurrence of old_str.
     start_pos = Pos(ls_mystring, ls_old_value, start_pos)



      MESSAGEBOX('before_pos_value',string(start_pos))    // my debug line
     


     // Only enter the loop if you find old_str.
     DO WHILE start_pos > 0
           // Replace old_str with new_str.
         ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
             messagebox('ls_mystring_after',ls_mystring)  //your debug line
     
           // Find the next occurrence of old_str.
           start_pos = Pos(ls_mystring, ls_old_value, start_pos+Len(ls_new_value))

               messagebox('AFTER_pos_value',string(start_pos))    // my debug line
      

ls_mystring is always having correct line.
The problem lies in the variable start_pos

It shows a value of 6 one time and then throught the loop it shows value of zero. The net result is that the replacement is made in one only one line of the entire file.

0
 
sandeep_patelCommented:
Hi

as i told you that there is no chance to repeat the date in a single line in your case, you can remove inner do while loop

let me put entire code again. Also while removing inner loop i forget to change one more thing.
Please copy this code. I have tested it, it's working fine. Only you need to change the name of files.


integer li_filenum,li_filenum2
string ls_line
blob lb_text
string ls_filename
string ls_temp

long start_pos=1
string ls_old_value, ls_new_value , ls_mystring

ls_old_value =  "12 05 2006"
ls_new_value= "2006/05/12"

ls_filename = 'c:\temp\test.txt'          // original file
ls_temp = 'c:\temp\test_copy.txt'     // Temporary file

li_filenum = FileOpen(ls_filename,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_temp,LineMode!,Write!,LockWrite!,Replace!)

FileWrite(li_filenum2,'kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover')
      
Do While FileRead(li_filenum,ls_line) > 0
      ls_mystring = ls_line
      // Find the first occurrence of old_str.
      start_pos = Pos(ls_mystring, ls_old_value, 1)
      // Only enter the loop if you find old_str.
      
      If start_pos > 0 Then
            // Replace old_str with new_str.
            ls_mystring = Replace(ls_mystring, start_pos, Len(ls_old_value), ls_new_value)
            ls_line = ls_mystring            
      End If

   FileWrite(li_filenum2,ls_line)
Loop

FileClose(li_filenum)
FileClose(li_filenum2)

li_filenum = FileOpen(ls_temp,LineMode!,Read!,LockWrite!)
li_filenum2 = FileOpen(ls_filename,LineMode!,Write!,LockWrite!,Replace!)

Do While FileRead(li_filenum,ls_line) > 0
   FileWrite(li_filenum2,ls_line)
Loop

FileClose(li_filenum)
FileClose(li_filenum2)

Regards,
Sandeep
0
 
MateenAuthor Commented:
Hi Sandeep,

This time it is 100% ok.

Thanks for all the valuable time u devoted 4 me.

My very best regards


Mateen
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.