Jump to content

Remove line of txt if a variable is in the line


Recommended Posts

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
Link to comment
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)
Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
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...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...