Link to home
Start Free TrialLog in
Avatar of Mateen
Mateen

asked on

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

Please read this first.
https://www.experts-exchange.com/questions/21848080/To-write-into-a-text-file.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"


ASKER CERTIFIED SOLUTION
Avatar of sandeep_patel
sandeep_patel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Mateen
Mateen

ASKER

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
https://www.experts-exchange.com/questions/21849882/To-download-a-file-from-within-pb7-script.html
Avatar of Mateen

ASKER

Hi sandeep

I have tested your code today.

Nothing happened.
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
Avatar of Mateen

ASKER

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.

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
Avatar of Mateen

ASKER

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.

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
Avatar of Mateen

ASKER

Hi Sandeep,

This time it is 100% ok.

Thanks for all the valuable time u devoted 4 me.

My very best regards


Mateen