remove the text (*@yahoo.com, hotmail.com) from one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting remove the text (*@yahoo.com, hotmail.com) from one file
# 1  
Old 06-15-2008
remove the text (*@yahoo.com, hotmail.com) from one file

Hi,

I have different file name (*) at the path: /path/* with the below content:

Example of the file content.
abc@test.com: abc@hotmail.com, abc@yahoo.com
xyz@test.com: xyz@hotmail.com
qwer@test.com: qwer@gmail.com,qwer@aol.com,qwer@test2.com
ghjk@test.com: ghjk@test2.com
abcd@xyz.com: abcd@hjkl.com, abcd@fghj.com,abcd@hotmail.com
..
..

I wish to remove those email account which have hotmail.com, yahoo.com,gmail.com, etc

Means after removing, the content will be as below:
abc@test.com:
xyz@test.com:
qwer@test.com: qwer@test2.com
ghjk@test.com: ghjk@test2.com
abcd@xyz.com: abcd@hjkl.com, abcd@fghj.com
..
..

I have an idea on how to get those line which have gmail.com, hotmail.com or yahoo.com by using the below command:

egrep -i '@yahoo.com|@gmail.com|@aol.com|@hotmail.com' /path/*

The output as below:
abc@test.com: abc@hotmail.com, abc@yahoo.com
xyz@test.com: xyz@hotmail.com
qwer@test.com: qwer@gmail.com,qwer@aol.com
abcd@xyz.com: abcd@hotmail.com
..
..

But, cant think of how to remove it directly from the files, can someone please advice on the issue?

It is it can be link with the command sed?

Thanks,
# 2  
Old 06-15-2008
Code:
tr ' ' '\n' < a | while read line
do
echo "$line" | egrep -v "hotmail|yahoo" 2>/dev/null 1>&2
if [ $? -eq 0 ]
then
echo "$line"
fi
done

# 3  
Old 06-15-2008
input-file:
Code:
abc@test.com: abc@hotmail.com,abc@yahoo.com
xyz@test.com: xyz@hotmail.com
qwer@test.com: qwer@gmail.com,qwer@aol.com,qwer@test2.com
ghjk@test.com: ghjk@test2.com
abcd@xyz.com: abcd@hjkl.com,abcd@gmail.com,bcd@mil.com

standard sed:
Code:
$ sed 's/\([ ,]\)[^@]\+@\(gmail\|yahoo\|hotmail\)\.com,\?/\1/g' input-file

sed with extended regex option (easier to read)
Code:
sed -r 's/([ ,])[^@]+@(gmail|yahoo|hotmail)\.com,?/\1/g' input-file

Output:
Code:
abc@test.com: abc@yahoo.com
xyz@test.com: 
qwer@test.com: qwer@aol.com,qwer@test2.com
ghjk@test.com: ghjk@test2.com
abcd@xyz.com: abcd@hjkl.com,bcd@mil.com

Note: I removed the space after the comma because sometimes there was one sometimes not. Not consistent.
# 4  
Old 06-15-2008
With Perl:

Code:
perl -i.bck -pe's/(?:,\s?)?\w+@(?:(?:hot|g)mail|aol|yahoo)\.com,?//g' /path/*


Last edited by radoulov; 06-15-2008 at 04:37 AM.. Reason: ... :)
# 5  
Old 06-15-2008
Wel, I just noticed my sed solution was not working for two consecutive banned email addresses. See first line of output. Give it a try with awk:

Code:
awk -F":" -v OFS=": " '{print $1, gensub(/[^@]+@(gmail|yahoo|hotmail)\.com,?/, "", "g", $2)}' input-file

# 6  
Old 06-15-2008
Quote:
Originally Posted by ripat
[...]
Give it a try with awk:
I think you mean GNU Awk.
# 7  
Old 06-15-2008
Code:
awk 'BEGIN{FS="[:,]"}
{
 for(i=1;i<=NF;i++) {
  if( $i !~ /yahoo|gmail|hotmail/) {
    printf $i" " 
  }  
 } 
 print ""
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove the text between all curly brackets from text file?

Hello experts, I have a text file with lot of curly brackets (both opening { & closing } ). I need to delete them alongwith the text between opening & closing brackets' pair. For ex: Input:- 59. Rh1 Qe4 {(Qf5-e4 Qd8-g8+ Kg6-f5 Qg8-h7+ Kf5-e5 Qh7-e7+ Ke5-f5 Qe7-d7+ Qe4-e6 Qd7-h7+ Qe6-g6... (6 Replies)
Discussion started by: prvnrk
6 Replies

2. Shell Programming and Scripting

Cut text from a file and remove

Hello Friends, I am stuck with the below problem.Any help will be appreciated. I have a file which has say 100 lines. On the second last line I have a line from which i want to remove certain characters.. e.g CAST(CAST( A as varchar(50)) || ',' || CAST(CAST( B as varchar(50)) || ',' ||... (8 Replies)
Discussion started by: vital_parsley
8 Replies

3. Shell Programming and Scripting

Search and remove in a text file

Need help whit a script where I have to input a name and then remove a line where that name is in a file file ex: 001op;Name;Location;date 002op;Name;Location;date and so on.... can anybody help me??? thanks (4 Replies)
Discussion started by: nogame11
4 Replies

4. Shell Programming and Scripting

Remove filename is text file

Hello, I got files full path in a text file like that /main/k/kdelibs/kdelibs4c2a_3.5.10.dfsg.1-2ubuntu7_i386.deb /main/k/kdelibs-experimental/libknotificationitem-dev_4.3.2-0ubuntu1_i386.deb /main/k/kdemultimedia/dragonplayer_4.3.2-0ubuntu1_i386.deb... (13 Replies)
Discussion started by: davidkhan
13 Replies

5. Shell Programming and Scripting

remove chunks of text from file

All, So, I have an ldif file that contains about 6500 users worth of data. Some users have a block of text I'd like to remove, while some don't. Example (block of text in question is the block starting with "authAuthority: ;Kerberosv5"): User with text block: # username, users,... (7 Replies)
Discussion started by: staze
7 Replies

6. Shell Programming and Scripting

able to remove \240 from a text file

I have a file originally provided from a SQL database on a Windows platform. I transfer the file via ftp in binary format, remove the ^M's from the end of all lines. I have attempted to use tr ( cat infile | tr -d '\240' ) and sed (cat infile | sed 's/\240//g' ) to remove the occurences of... (11 Replies)
Discussion started by: pbrowne
11 Replies

7. Shell Programming and Scripting

remove and replace text in a file

Hello all, How would I go to a particular line in a file and remove certain text from it and replace with something that I want it to be there. like: file /etc/abc now look for line HOME="/export/xyz" in /etc/abc and then replace with HOME=/"export/xyz1" thanks in advance guys. (1 Reply)
Discussion started by: solaix14
1 Replies

8. Shell Programming and Scripting

remove specified text from file

I am trying to write a script that kills old sessions, I've posted here over the past few days and the script is just about perfect except I want to be given the option to exclude specified PIDs from being killed. this is the entire script: if then rm /tmp/idlepids fi if then rm... (2 Replies)
Discussion started by: raidzero
2 Replies

9. UNIX for Dummies Questions & Answers

Send mail to any user (@hotmail,@yahoo etc) from Solaris

Hi All, I am new to unix. I want to know what are the steps to do in order to configure a unix server (Solaris 8) to send mails to the outside world. I also want to send attachments in the mail. Thanks in advance Zing (2 Replies)
Discussion started by: zing
2 Replies
Login or Register to Ask a Question