简体   繁体   中英

Use linux grep to find emails

If this question has been asked and answered before, my apologies. I couldn't find anything from looking.

How can I use linux grep / regex to find unknown characters in an email address? For example, let's say we had this list:

userone:123456@example.com

usertwo:123@example.com

userthree:12@example.com

how could I grep the list to find emails matching ***@example.com ? (the only email that should be found from this is 123@example.com)

I'm aware that grep -e '...@example\\.com' would work, but periods can represent any characters in grep, so doing this would also find :12@example.com . Plus, MOST email address don't contain just any character, they are typically confined to letters, numbers, periods, and underscores (many email providers don't allow anything else)

I need to use something else besides a period symbol in grep, something like [a-Z0-9._] so that letters, numbers, periods, and underscores are included but nothing else. I'm unsure of how to go about this. Thanks

EDIT: What I've tried so far: grep -e '[a-zA-Z0-9_.]{3}@example.com' * . This doesn't work, so it comes down to just me getting the regex wrong.

If the email addresses are always preceded by a username, which is then followed by a colon or a space and then the email address, you can use that knowledge to restrict your matches.

What does a username look like? You need to know if you're going to use it to find matches. Let's say for now it is letters, numbers, dash, and underscore, it always starts with a letter, and is from 2 to 12 characters long. We also know it's got a colon or space after it. The regex for that is

[A-Za-z][A-Za-z0-9_-]{1,11}[: ]

That would be followed by your email address which, it sounds like, is something you decide on and input because that's what you're looking for at the moment.

Your example of test*****@example.com would be

[A-Za-z][A-Za-z0-9_-]{1,11}[: ]test.\+@example.com

or, if exactly 5 chars after "test"

[A-Za-z][A-Za-z0-9_-]{1,11}[: ]test.....@example.com

Your original sample ***@example.com is "any 3-char address at example.com" and would be

[A-Za-z][A-Za-z0-9_-]{1,11}[: ]...@example.com

This would be a pain to retype that prefix all the time, so you'd want to wrap that in a script that uses prefix + what_i_typed as the pattern.

试试这个命令行,我曾经在任何文件中找到任何东西

 grep -r -i @example.com ./

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM