LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2013, 11:13 AM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
deleting lines with matching variable


I have a lists of emails that I need to delete.

e.g.

john@foo.com
jean@foo.com
mark@foo.com.cn

I need to remove the line that match the variable "foo.com". I have created a script but it also remove mark@foo.com.cn.

Code:
echo ""
echo -n "What Domain: "
read domain
echo ""
echo -n "Removing Domain $domain" "? [y/n]: "
read confirm
        if [ $confirm == "y" ];then
                grep -v @$domain $ADDRESSES > $ADDRESSES_TMP
                cat $ADDRESSES_TMP > $ADDRESSES
        elif [ $confirm == "n" ];then
                echo "Exiting"
                exit
        else
                echo "Wrong Input. Please use y or n"
        fi
Any suggestions how to delete those lines matching foo.com without deleting foo.com.cz (or any characters after the given variables)

TIA
 
Old 06-29-2013, 11:39 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by packets View Post
Any suggestions how to delete those lines matching foo.com without deleting foo.com.cz (or any characters after the given variables)
grep can do this nicely.

With this InFile ...
Code:
john@foo.com
jean@foo.com
mark@foo.com.cn
paul@moo.com
... this grep ...
Code:
 grep -v foo.com$ $InFile >$OutFile
... produced this OutFile...
Code:
mark@foo.com.cn
paul@moo.com
The $ requires a comparison on foo.com at the end of the line.
The -v says keep the lines which do not match.


Daniel B. Martin
 
1 members found this post helpful.
Old 06-29-2013, 12:01 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Grep or sed:

Code:
sed -n '/@foo.com$/p' infile.txt > outfile.txt

Please note I included @ just in case you have emails with domains such as moofoo.com
 
2 members found this post helpful.
Old 06-30-2013, 12:01 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You could also do it all in bash with a simple loop. You would need to be clearer about the data:

1. Is is at the end of a line? otherwise Daniel's suggestion will not work

2. As pointed out by post above, is there a chance of a match on a sub name, like moofoo and foo

Lastly, when using variables in bash which may have any unusual characters, they should be quoted.
 
1 members found this post helpful.
Old 06-30-2013, 12:51 AM   #5
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
> 2. As pointed out by post above, is there a chance of a match on a sub name, like moofoo and foo

Yes. There might be a chance that this might happen. That is why I put "@" before the foo.com just to make sure it would not match moofoo.com

> 1. Is is at the end of a line? otherwise Daniel's suggestion will not work

I am not sure if I get it right but I assume yes because in my example, it is a lists of email address.

john@foo.com
jean@foo.com
mark@foo.com.cn

Looks like Daniel's suggestion works. Why I forgot the $ geezz

Code:
[root@packets tmp]# cat users.txt 
john@foo.com
jean@foo.com
jean@moofoo.com
mark@foo.com.cn
[root@packets tmp]# cat users.txt | grep -v "@foo.com$"
jean@moofoo.com
mark@foo.com.cn
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] deleting iptables rules by matching mark siddhiqbcs Linux - Newbie 1 09-19-2012 08:12 AM
[SOLVED] bash matching two files lines by lines rperezalejo Programming 6 01-12-2012 06:42 AM
deleting duplicate lines without deleting first instance of the duplicated line jkeertir Linux - Newbie 2 02-07-2011 06:55 AM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration