Page 1 of 1
removing key word lines from text file
Posted: Sun Mar 05, 2017 4:00 pm
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?
Re: removing key word lines from text file
Posted: Sun Mar 05, 2017 4:21 pm
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.
Re: removing key word lines from text file
Posted: Sun Mar 05, 2017 4:22 pm
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

Re: removing key word lines from text file
Posted: Sun Mar 05, 2017 4:36 pm
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
Re: removing key word lines from text file
Posted: Sun Mar 05, 2017 5:05 pm
by Marc56us
Yes, and possible improved alternative
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

Re: removing key word lines from text file
Posted: Mon Mar 06, 2017 7:13 pm
by smacker
Good suggestions all, thanks folks.
After checking out each on I ended up using the regex method.