-   Linux - General (
-   -   sed or grep : delete lines containing matching text (

raj000 05-20-2006 03:58 AM

sed or grep : delete lines containing matching text

I have been struggling with this for really long.

I want to match text patterns from a file and delete all lines in a 2nd file that contain the matching pattern from the first file.

I have 2 files , namely "emails" and "delemails" . "emails" contains a list of 2000 emails and "delemails" contains a list of 200 emails that need to be deleted from "emails".

I know i can do it using sed or grep -v option...but can't get the syntax right.

Would appreciate your help.

pixellany 05-20-2006 06:07 AM

grep simply finds lines containing a particular pattern. grep -v lloks for line that do NOT contain the pattern. Not sure how that relates to your problem....

sed is a relatively complex function. At its core is is used to find and change patterns of any size, but it has a whole bunch of options. Here is a very good tutorial:

You may also want to look at awk.

When dealing with two different files, it may be easier to have a script that opens one file, finds the pattern of interest and then feeds that to the function that is going to operate on the other file.

If you post some of your actual code, we may be able to be more helpful on the approach you are trying.

////// 05-20-2006 07:54 AM

grep -Ev 'crap0|crap1|crap2|crap3'

That will delete lines containing crap[0-3].

spirit receiver 05-20-2006 11:27 AM

This is just a nonsense post because the forum won't let me post URLs until I posted at least 3 messages. So see below.:D

spirit receiver 05-20-2006 11:29 AM

Are you saying that your files are mbox files containing entire messages? Then simple pattern matching on separate lines won't be very helpful. To me it seems more reasonable to write a Perl script using modules from that are designed to handle email properly (have a look at Email::Folder and Email::LocalDelivery).

////// 05-20-2006 06:53 PM

# [make dictionary]
# Modification of /usr/sbin/mkdict script.
# Original script copyright 1993, by Alec Muffett.
# This modified script included in this document in a manner consistent with the
# "LICENSE" document of the "Crack" package that the original script is a part of.
# This script processes text files to produce a sorted list of words found in the files.
# This may be useful for compiling dictionaries and for lexicographic research.
#Usage: /root/Desktop/ files-to-process
# or in this case :
#cat /root/Desktop/delemails /root/Desktop/emails > /root/Desktop/cattedlist
# NOTICE look at /root/Desktop/cattedlist , there is one line that could have
# 2 email addresses combined like this :
#/root/Desktop/ /root/Desktop/cattedlist > /root/Desktop/cleanlist.txt


if [ ! -r "$1" ] # Need at least one
then # valid file argument.
echo "Usage: $0 files-to-process"

cat $* | # Contents of specified files to stdout.
# tr A-Z a-z | # Convert to lowercase.
tr ' ' '\012' | # Change spaces to newlines.
# tr -c '\012a-z' '\012' |
sort |
# uniq | # Remove duplicates.
uniq -u|
grep -v '^#' | # Delete lines beginning with a hashmark.
grep -v '^$' # Delete blank lines.

exit 0

raj000 05-21-2006 05:55 AM


Thanks a ton for your replies. Actually the 2 files are fairly simple text files. Each file contains email addresses in separate row.

File1 -: (this contains about 2000 mails)

File2 -: (this contains about 200 mails that should be deleted from file1)

in the above example, i need a sed command that would take inputs from file2, 1 line at a time, delete and from file1 and output the result to file3.

raj000 05-22-2006 12:57 AM

Actually, grep -Ev 'crap0|crap1|crap2|crap3' is very close to what i want, but i want to input the regular expressions from a file. Ouput will be fairly simple like >>file3

////// 05-22-2006 01:30 AM

If this is what you want to do, you can do it with that script.
What that script does is that it cat's them is one big file [emails+delemails], then it looks for duplicates [uniq -u] and outputs only (it deletes duplicates) unique email addresses, and if you have catted both emails and delemails you end up with email list that doesnt anymore contain those emails that you wanted to delete.

So, first cat'em:

cat /path/to/delemails /path/to/emails > /path/to/DelemailsAndMails.txt

/path/to/ /path/to/DelemailsAndMails.txt > /path/to/cleanlist.txt
And that "cleanlist.txt" will contain only those emails you wanted. Just save that script as "" and modify the paths and it will work, I tested it ;-)

raj000 05-26-2006 04:56 AM

hmmm...very promising solution . Its very close to what i wanted , except for one thing.

The file "delemails" may contain some emails that do not exist in the file "emails". I want these to be ignored. In the solution you have mentioned, mails that are in "delemails" but not in "emails" will not be duplicate. So when i get the output of "cleanlist", it will contain emails that existed in "delemails", but did not exist in "emails"

Is there not a sed command that takes the input from a file to delete emails?

I could use excel to append a "-d" (or whatever) to the beginning of each line in delemails so it looks like -:



spirit receiver 05-27-2006 05:05 AM

As a solution in that line of thought, you could first pipe both files to "(sort | uniq -d)", which will return only those lines that appear twice. This may then be subtracted from the first file as before.
But you'll have to make sure that neither of the two input files contains duplicate lines (possibly by using "uniq -u" on each file separately).

nitin_nitt 09-25-2008 03:02 AM

Too late a reply, but if someone has a similar problem, here's a neat one-liner to solve this in bash:


diff file1 file2 | sed '/^[0-9][0-9]*/d; s/^. //; /^---$/d' > file3
This gives all the lines which are not common to the two files.

Sorted mail-lists have a simpler solution:


comm -23 file1 file2 > file3
This gives all the lines in file1 that are not present in file2.

asnani_satish 08-02-2009 09:44 PM

search for string and delet files
grep -lir 'string to search in files' * |xargs rm -rf

l - for listing file name only
i - ignore case while searching
r - search recursively within sub directories also
xargs - the list of files from grep command are passed as parameter to "rm -rf" command

dougsk 12-06-2009 01:00 AM


Originally Posted by ////// (Post 2253980)
grep -Ev 'crap0|crap1|crap2|crap3'

That will delete lines containing crap[0-3].

beautiful. You saved me 406 lines of deletion!

I'm going to remember this.



cezam 01-06-2010 10:35 AM

Same problem
Hi i got the same problem here i want to delete each lines of codes that has H3qqea3ur6p in it which is a virus thats infecting people browsing a site that im hosting.. anyhow the grep -Ev doesn't seem to work for me any suggestions???

i had done 1400 lines of removing code so far lol.. but 700 are still not done.


All times are GMT -5. The time now is 08:42 AM.