Author Topic: [SOLVED] Search and replace in file  (Read 140 times)

smil3y

  • Newbie
  • *
  • Posts: 2
[SOLVED] Search and replace in file
« on: January 07, 2012, 09:02:04 PM »
Hello, dead all!

How can I search and replace a string in a file (text file)? I want to have a function to handle that. I can use SHELL with sed but I don't consider that better than using Gambas for that. I can search in the file with the following function:

Code: [Select]
PUBLIC FUNCTION Get_Str(tmpFile AS String, tmpStr AS String) AS Variant
  DIM sLine, Result AS String
  DIM hFile AS File
 
  hFile = OPEN tmpFile FOR INPUT
  WHILE NOT Eof(hFile)
    LINE INPUT #hFile, sLine
    IF sLine LIKE "*" & tmpStr & "*" THEN
      sLine = Replace$(sLine, "\"", "")
      sLine = Replace$(sLine, "export ", "")
      Result = Replace$(sLine, tmpStr, "")
      BREAK
    ENDIF
  WEND
 
  RETURN Result
 
  CATCH
    Message.Error("Unable to get info!\n\nFile: " & tmpFile & "\nString: " & tmpStr)
END

But how about replacing strings? I'm not an expert so a few lines of comments will be helpful too.

Thanks in advance!

PS: This is just a function from my applications
« Last Edit: January 09, 2012, 02:41:00 PM by smil3y »

Linux Basic

[SOLVED] Search and replace in file
« on: January 07, 2012, 09:02:04 PM »

smil3y

  • Newbie
  • *
  • Posts: 2
Re: Search and replace in file
« Reply #1 on: January 09, 2012, 02:40:45 PM »
Ok, I'll answer my own question  :P

Replacing specific string in file is very simple:
Code: [Select]
DIM content as String

' read the content of the file
content = File.Load("/path/to/file.ext")

'replace the string "old" with "new" (without quotes) in the content of the file
content = Replace$(content, "old", "new")

' save the new content to file
File.Save("/path/to/file.ext",content)
This can be 2-lines code but I prefered to explain how it's done (or at least the way I figured out)

However, if you don't know the exact word you want to replace (you want to search for pattern match) things get complicated, here is the function I use now:
 
Code: [Select]
PUBLIC FUNCTION Replace_Str(SrchFile AS String, SrchStr AS String, RpcStr AS String)
  DIM content_array AS String[]
  DIM sLine, content AS String
  DIM counter AS Integer
 
  ' load file content
  content = File.Load(SrchFile)
  ' split the content of the file to array with new line as delimiter
  content_array = Split(content, "\n")
 
  counter = 0
  ' read file line by line
  FOR EACH sLine IN content_array
    ' match pattern search agains SrchStr
    IF sLine LIKE SrchStr & "*" THEN
      ' save file content if SrchStr found and replace
      File.Save(SrchFile, Replace$(content, sLine, RpcStr))
     
      ' stop here, no need to search more (for me. to replace globaly remove BREAK)
      BREAK
    ENDIF
     
    counter += 1
  NEXT
 
' if the last lines was reached and still there wasn't match pop-up error message (remove if replacing globaly)
  IF counter = content_array.Count THEN
    Message.Error("Unable to find:\n\nString: " & SrchStr & "\n\n In: " & SrchFile)
  ENDIF
END

I hope this helps other Gambas users :)

More about pattern syntax : http://gambasdoc.org/help/doc/pcre

Cheers!

EDIT: If replacing globaly then exclude the error message too (noted in code)
« Last Edit: February 13, 2012, 05:48:03 PM by smil3y »