LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   deleting lines with matching variable (https://www.linuxquestions.org/questions/programming-9/deleting-lines-with-matching-variable-4175467828/)

packets 06-29-2013 11:13 AM

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

danielbmartin 06-29-2013 11:39 AM

Quote:

Originally Posted by packets (Post 4980798)
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

sycamorex 06-29-2013 12:01 PM

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

grail 06-30-2013 12:01 AM

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.

packets 06-30-2013 12:51 AM

> 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



All times are GMT -5. The time now is 06:56 PM.