[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-12
10
Medium Priority
?
834 Views
Last Modified: 2013-12-26
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"


0
Comment
Question by:Mateen
  • 5
  • 5
10 Comments
 
LVL 14

Accepted Solution

by:
sandeep_patel earned 2000 total points
ID: 16676781
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
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 16676808
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
 

Author Comment

by:Mateen
ID: 16677699
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Mateen
ID: 16679794
Hi sandeep

I have tested your code today.

Nothing happened.
0
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 16679974
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
 

Author Comment

by:Mateen
ID: 16680104
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
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 16687649
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
 

Author Comment

by:Mateen
ID: 16688939
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
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 16691598
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
 

Author Comment

by:Mateen
ID: 16696543
Hi Sandeep,

This time it is 100% ok.

Thanks for all the valuable time u devoted 4 me.

My very best regards


Mateen
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question