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 "" TIA |
Quote:
With this InFile ... Code:
john@foo.com Code:
grep -v foo.com$ $InFile >$OutFile Code:
mark@foo.com.cn The -v says keep the lines which do not match. Daniel B. Martin |
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 |
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. |
> 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 |
All times are GMT -5. The time now is 12:00 AM. |