How to delete lines if it contain certain character ?

MethShow

Registered Member
Joined
Jan 24, 2011
Messages
57
Reaction score
2
How to delete lines if it contain certain character ?

Let's say, I have a text file like this -

aaa aaa
bbb bbb
ccc ccc e
ddd ddd
fff fff e

Now, I want to remove all the lines that contain the word "e", so after I run the software, I will get -

aaa aaa
bbb bbb
ddd ddd

Do you guys know any software or method that can do this ?

Thanks
 
I'd say RegEx if you're a php coder.

Or you can also do this easily in excel with a formula using FIND, ISNUMBER and IF.
 
Off the top of my head, I'd use a spreadsheet for this.

Load up your text file into a spreadsheet, then create a formula in a column (right or left...it doesn't matter) that searches for the string and "flips a switch" if found (using and if statement). You can then sort the entire list by this field and delete all the rows in one fell swoop. Then it's just a matter of saving the file back out with just the rows you need.
 
Here you go:

Create new PHP file, lets say clean.php and paste this code:

PHP:
<?php
$rows = file("FileName.txt");    
$blacklist = "TheWordYouWantToRemove";

foreach($rows as $key => $row) {
    if(preg_match("/($blacklist)/", $row)) {
        unset($rows[$key]);
    }
}

file_put_contents("solved.txt", implode($rows));

?>
Change FileName.txt and TheWordYouWantToRemove with your txt file name and string and upload clean.php and your text file to your hosting account, visit the URL where the files are ... the cleaned file will be saved as "solved.txt" on your server.

Hope this helps!

Wow man, you are the man.:)
 
Back
Top