Sign in to follow this  
Followers 0
60aside

Remove line of txt if a variable is in the line

11 posts in this topic

#1 ·  Posted (edited)

Hi,

I nearly have this working but not quite,

I have a text file $text and another text file $file3 that I read line by line and assign the variable $remove to each line like this:-

For $i = 1 to _FileCountLines($file3)
    $remove = FileReadLine($file3, $i)

I want to completely remove the line in $text if $remove exists (case insensitive)

This line of code below is producing wierd results, :-

StringRegExpReplace($text,"(?i)\h*\Q" & $remove & "\E.*\cM\cJ","")

Can anyone help?

Thanks.

Edited by 60aside

Share this post


Link to post
Share on other sites



So you are reading $file3 line-by-line then if that line occurs anywhere in $text you want it removed?

StringRegExpReplace works on strings, not on files or file handles, is $text a string in your script?

Share this post


Link to post
Share on other sites

So you are reading $file3 line-by-line then if that line occurs anywhere in $text you want it removed?

StringRegExpReplace works on strings, not on files or file handles, is $text a string in your script?

I read the whole file

$text = FileRead($file)

Share this post


Link to post
Share on other sites

So what kind of weird results are you getting?

Maybe try replacing your "\cM\cJ" with a "\v"?

Share this post


Link to post
Share on other sites

if the variable is in the line of text, it will remove the variable and everything on the line to the right of the variable.

But anything to the left of the variable will then be added to the next line of text.

Like if the variable is .net and the file is:-

Microsoft .net framework

Adobe Reader 9.0

after the script has run it will be

MicrosoftAdobe Reader 9.0

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Oh yea...in your StringRegExpReplace, make the ReplaceString @CRLF instead of "".

Add:

...Either that or don't replace the carriage return and/or line feed in the first place.

Edited by MrMitchell

Share this post


Link to post
Share on other sites

Oh yea...in your StringRegExpReplace, make the ReplaceString @CRLF instead of "".

Add:

...Either that or don't replace the carriage return and/or line feed in the first place.

That seems to give me :-

Microsoft

Adobe Reader 9.0

what I need is:-

Adobe Reader 9.0

- I'm so close .. any other ideas? I need it to completely delete the whole line if my variable is present anywhere in the line.

Thanks.

Share this post


Link to post
Share on other sites

Add another ".*" in your RegEx before $remove:

StringRegExpReplace($text,"(?i)\h*.*\Q" & $remove & "\E.*\v","")

Share this post


Link to post
Share on other sites

Add another ".*" in your RegEx before $remove:

StringRegExpReplace($text,"(?i)\h*.*\Q" & $remove & "\E.*\v","")

Spot on mate.

Thanks a lot.

Share this post


Link to post
Share on other sites

I have found that I have some duplicate entries in $text.

Is there a way to remove these and just keep the 1st entry?

A quick search comes up with _ArrayUnique, is this the way to go with this?

Thanks again.

Share this post


Link to post
Share on other sites

I have found that I have some duplicate entries in $text.

Is there a way to remove these and just keep the 1st entry?

A quick search comes up with _ArrayUnique, is this the way to go with this?

Thanks again.

Others could probably chime in and provide better suggestions, but if there are duplicate lines then you may be able to use _ArrayUnique(), but you need the whole $text into an array first. Then the new array would have be output element-by-element to a new file to get back to where you started. This might not even be feasible depending on how big the $text is...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0