"SED" it is Not!

Windows specific forum
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

"SED" it is Not!

Post by Randy Walker »

"sed" is a Linux bash command that is very powerful. One thing I really liked about it was the ability to swap text inside an ascii file. I've often wanted to be able to do that in Windows but the native resource to do that in Windows is quite complex, so this is my latest creation. I call it "SEDitisNot" because it is not "sed" but fits the bill if you only want to swap text in an ascii file. Thanks to help I received on the findstring() command recently I was able to get this working as I hoped. Hell, this might even be cross platform if compiled on Linux, but you already have the real "sed".
BTW... I should mention this code did not work in PB5.40, 5.62, 6.20 and didn't work until I ran it in PB6.21

Code: Select all

;{- Enumerations / DataSections
;{ Window
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Text_0
  #Search
  #Replace
  #Text_5
  #Text_6
  #CheckBox_7
  #Ok_button
  #Cancel_Button
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
StandardFile$ = "C:\ "
Pattern$ = "Text (*.txt;*.bat)|*.txt;*.bat |PureBasic (*.pb)|*.pb|All files (*.*)|*.*"
Pattern = 0 
filename$ = OpenFileRequester("sed style find&replace",StandardFile$,Pattern$,Pattern)
If filename$ =""
  End
EndIf
Debug filename$
  If OpenWindow(#Window_0, 424, 193, 400, 170, "Search & Replace Criteria", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    TextGadget(#Text_0, 15, 10, 365, 35, "Similar to Linux 'sed' command; This will find and replace each occurance of your search term.  Search critieria is case sensitive.")
    StringGadget(#Search, 120, 55, 255, 25, "")
    StringGadget(#Replace, 120, 85, 250, 20, "")
    TextGadget(#Text_5, 35, 55, 70, 15, "Search")
    TextGadget(#Text_6, 35, 85, 65, 15, "Replace")
    CheckBoxGadget(#CheckBox_7, 70, 110, 240, 25, "Line begins with search term")
    ButtonGadget(#Ok_button,140,140,50,30," OK ")
    ButtonGadget(#Cancel_Button,230,140,70,30," Cancel ")
  EndIf
Repeat
  Event = WaitWindowEvent()
  Select Event
      ; ///////////////////
    Case #PB_Event_Gadget
      Etype = EventType()
      Select EventGadget()
        Case #Ok_button
          Debug "Want to process"
          process = #True
          Break
        Case #Cancel_Button
          CloseWindow(#Window_0)
          End
      EndSelect
      ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        End
      EndIf
  EndSelect
ForEver

search.s = GetGadgetText(#Search)
replace.s = GetGadgetText(#Replace)
If GetGadgetState(#CheckBox_7)
  Debug "Checked"
  search = #CRLF$+Mid(search,3)
  Debug search
  replace = #CRLF$+replace
EndIf

If process
  If OpenFile(0,filename$)
    Debug "Opened file OK"
    text.s = ReadString(0,#PB_Ascii | #PB_File_IgnoreEOL)
    s.s = ReplaceString(text,search,replace)
    Debug Len(text)
    FileSeek(0,0)
    WriteString(0,s)
    CloseFile(0)
  EndIf
  Debug Len(text)
  If FindString(text,search) = 0
    MessageRequester("Maybe Try Again?", "Your search term was not found in the file." + Chr(10) + "Try again and double check your entry.", #MB_OK|#MB_ICONWARNING)
  EndIf
EndIf
End
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Rinzwind
Enthusiast
Enthusiast
Posts: 690
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: "SED" it is Not!

Post by Rinzwind »

I know it's about the PB code... but SED for Windows:
https://github.com/mbuilov/sed-windows/releases
Post Reply