How do i erase all line if i find certain text?

Tool for Search and Replace across multiple files.
Post Reply
User avatar
Gastons

How do i erase all line if i find certain text?

Post by Gastons »

I find word 'water' in 55 files.
How do I erase all lines which contain word 'water' in all 55 files?
User avatar
SpiroC

Post by SpiroC »

This should work, but you should test it first (make copies of files, check results with a compare utility):

Regular Expression:
Reg Ex: .*water.*[\r\n]+

Replace:

Note: leave replace empty.
User avatar
DV

Post by DV »

That'll do it - just be sure that 'Dot Matches Newline' is not ticked.
You can try it out using the regex tester in textcrawler.
User avatar
Fool4UAnyway

Post by Fool4UAnyway »

Wouldn't it be safer to put it this way? :

^.*water.*$
User avatar
Fool4UAnyway

Post by Fool4UAnyway »

OK, it occurred to me what the difference is between the two proposed regular expressions.

^.*water.*$

This one will only match the _contents_ of lines containing the word water and replacing those with nothing will _clear_ those lines, not delete them. You will be left with _empty_ lines where a match was found.

.*water.*[\r\n]+

This one, on the other hand may also include empty lines _after_ each line that contains the word water, because "any number of consecutive newline characters, with at least one" will be accepted.

I don't know how to include only one single newline character that could be _either_ \r _or_ \n _or_ \r\n.
User avatar
Fool4UAnyway

Post by Fool4UAnyway »

> I don't know how to include only one single newline
> character that could be _either_ \r _or_ \n _or_ \r\n.

Just reading what I just wrote made me realise this is just how you should build the regular expression:

(\r|\n|\r\n)

So this one would be like perfect:

.*water.*(\r|\n|\r\n)
Post Reply