need help finding a specific email address in a file and deleting the line its on.

classic Classic list List threaded Threaded
6 messages Options
MJ
Reply | Threaded
Open this post in threaded view
|

need help finding a specific email address in a file and deleting the line its on.

MJ
Hi,

I'm new to this and I'm trying to create a script that will take a user supplied email address and find it in a file of email addresses (unique addresses, each address is at the start of its line.) and delete the line and save (replace) the file.

Any help would be appreciated.

- MJ
Reply | Threaded
Open this post in threaded view
|

Re: need help finding a specific email address in a file and deleting the line its on.

Guru
Administrator
  Please provide a sample input file and expected output file. Also show us what you have tried so far?
MJ
Reply | Threaded
Open this post in threaded view
|

Re: need help finding a specific email address in a file and deleting the line its on.

MJ
Sorry for the delayed reply, got into watching the Orion mission.

Ok this is an example (small sample, the actual file contains thousands of email addresses) of the file I'll be accessing:

user3@companya.com [domain.user.biz]:GWS
user26@companyc.com [domain.user.biz]:GWS
user22@companyc.com [domain.user.biz]:GWS
user18@companyb.com [domain.user.biz]:GWS
user11@companya.com [domain.user.biz]:GWS
user10@companyx.com [domain.user.biz]:GWS
user16@companya.com [domain.user.biz]:GWS
user5@companyl.com [domain.user.biz]:GWS
user7@companyb.com [domain.user.biz]:GWS
user2@companyq.com [domain.user.biz]:GWS
user0@companya.com [domain.user.biz]:GWS
user1@companyb.com [domain.user.biz]:GWS
user8@companyd.com [domain.user.biz]:GWS
user12@companyk.com [domain.user.biz]:GWS
user17@companyw.com [domain.user.biz]:GWS
user20@companyw.com [domain.user.biz]:GWS
user19@companya.com [domain.user.biz]:GWS
user25@companya.com [domain.user.biz]:GWS
user21@companyb.com [domain.user.biz]:GWS
user23@companya.com [domain.user.biz]:GWS
user24@companyx.com [domain.user.biz]:GWS
user14@companyc.com [domain.user.biz]:GWS
user9@companyk.com [domain.user.biz]:GWS
user13@companyb.com [domain.user.biz]:GWS
user15@companyc.com [domain.user.biz]:GWS
user6@companya.com [domain.user.biz]:GWS
user4@companyz.com [domain.user.biz]:GWS

There are other applications that will insert email addresses at the top of the file so the most recent addition is away at the top.  I can’t sort the file for this reason.  I need to search for the email address and then delete its line from the file.   I’ve tried working with sed, but it sends its output to screen and it won't (or I don't know how to get it to) save to an existing file.  I’m trying to avoid redirection to a to a temp file and then writing it back as there is potential there to overwrite an entry that was inserted.

Suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: need help finding a specific email address in a file and deleting the line its on.

Guru
Administrator
$ cat email
user3@companya.com [domain.user.biz]:GWS
user26@companyc.com [domain.user.biz]:GWS
user22@companyc.com [domain.user.biz]:GWS
user18@companyb.com [domain.user.biz]:GWS
user11@companya.com [domain.user.biz]:GWS
user10@companyx.com [domain.user.biz]:GWS
$
$ sed -i '/user3@companya.com/d' email
$ cat email
user26@companyc.com [domain.user.biz]:GWS
user22@companyc.com [domain.user.biz]:GWS
user18@companyb.com [domain.user.biz]:GWS
user11@companya.com [domain.user.biz]:GWS
user10@companyx.com [domain.user.biz]:GWS
$
MJ
Reply | Threaded
Open this post in threaded view
|

Re: need help finding a specific email address in a file and deleting the line its on.

MJ
Thank you that got me on the right track.
Reply | Threaded
Open this post in threaded view
|

Re: need help finding a specific email address in a file and deleting the line its on.

Raghwendra Kumar Mishra
In reply to this post by Guru
Suppose file is :
user1@mail.com
user2@mail.com
user3@mail.com

To search a mail id and delete that line:
sed '/<mail_id>/d' file
e.g.
sed '/user2@mail.com/d' file

To insert at the start of line:
sed '1i <mail_id> file
e.g.
sed '1i user0@mail.com' file