removing key word lines from text file

Everything else that doesn't fall into one of the other PB categories.
smacker
User
User
Posts: 55
Joined: Thu Nov 06, 2014 7:18 pm

removing key word lines from text file

Post by smacker »

If I have this, for example, in a text file

Code: Select all

The dog and the cat ran
The cat and the bird
The dog and the bird ran
The mouse and the cat ran
The dog and the mouse
What is the best way to remove any lines (the complete line) in the text file that contain the keyword dog without having to resort to using multiple instances of RemoveString(...) containing the whole line for each individual line to be removed? In other words, remove any line that contains the keyword dog. Then how to do it if there are multiple keywords in a line and you want to remove that line completely if it contains any of the key words?
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: removing key word lines from text file

Post by skywalk »

Search forum for Split string, then Removestring() dog from each line in array. Then search forum for Join string to put array back to 1 string.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: removing key word lines from text file

Post by Bisonte »

the easy way :

Code: Select all

Define File, KeyWord.s, String.s
Define NewList Row.s()

File = ReadFile(#PB_Any, "TextFile")

KeyWord = "dog"

If File
  While Not Eof(File)
    String.s = ReadString(File)
    If Not FindString(String, KeyWord, #PB_String_NoCase)
      AddElement(Row()) : Row() = String
    EndIf
  Wend
  CloseFile(File)
EndIf
Edit : ok, I forget the flag : #PB_String_NoCase at FindString ;)
Last edited by Bisonte on Sun Mar 05, 2017 4:47 pm, edited 2 times in total.
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: removing key word lines from text file

Post by Zebuddi123 »

Alternatives :

Code: Select all

s.s = "The dog and the cat ran"  + #CRLF$ +
      "The cat And the bird" +  #CRLF$ +
      "The dog And the bird ran" + #CRLF$ +
      "The mouse And the cat ran" + #CRLF$ +
      "The dog And the mouse" + #CRLF$ 

For i = 1 To CountString(s, #CRLF$)
	sString.s = StringField(s,i, #CRLF$)
	If Not FindString(sString,"dog") And  Not FindString(sString,"Dog")   ; Matchs Dog or dog
		Debug sString
	EndIf
Next

iRegex.i = CreateRegularExpression(#PB_Any, "[Dd]og")  ; Matchs Dog or dog
For i = 1 To CountString(s, #CRLF$)
	sString.s = StringField(s,i, #CRLF$)
	If Not MatchRegularExpression(iRegex, sString)
		Debug sString
	EndIf
Next
malleo, caput, bang. Ego, comprehendunt in tempore
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: removing key word lines from text file

Post by Marc56us »

Yes, and possible improved alternative :P

Code: Select all

iRegex.i = CreateRegularExpression(#PB_Any, "\bdog\b", #PB_RegularExpression_NoCase)  
; Matchs dog whatever letters case
; Matchs dog preceded and / or followed immediately (without space) by ,;. <tab> etc
; Does not match "dogs" "hotdog"
I like regex too 8)
smacker
User
User
Posts: 55
Joined: Thu Nov 06, 2014 7:18 pm

Re: removing key word lines from text file

Post by smacker »

Good suggestions all, thanks folks. :)

After checking out each on I ended up using the regex method.
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
Post Reply