grep regex to find emails of a certain tld
Do:
grep '@.\+\.com$' file.txt
@.\+
matches a@
followed by one or more characters\.com$
matches literal.com
at the end
to do the same for other TLDs, replace com
at the end with that TLD.
grep regex match email address
The problem is that grep
matches anything in a line. If you want the exact whole line, add the $
terminator at the end. Let's look at an example:
ABCabc+-._@school3-IT.image.tor.chrome.canadannn
ABCabc+-._
matches^[A-Za-z0-9+._-]+
@
matches@
school3-IT.image.tor.chrome.
matches([a-zA-Z0-9-]+\.)+
. As far as I know, all quantifiers are greedy ingrep
.canada
matches[a-zA-Z]{2,6}
nnn
gets ignored
Without the $
, there just has to be some part of the line that matches, not necessarily the whole thing.
Extract email addresses from log with grep or sed
With GNU grep, just use a regular expression containing a look behind and look ahead:
$ grep -Po '(?<=to=<).*(?=>)' file
wanted1918_ke@yahoo.com
devi_joshi@yahoo.com
This says: hey, extract all the strings preceded by to=<
and followed by >
.
How to extract emails from logs using grep
You can use awk
to both check if the "Successfully done something." line occurs and also it contains an email:
awk '/Successfully done something. Email:/ && \ # match line
match($0, /Email: ([^ ]*) /, matches) { # match up to space
print matches[1] # print captured group
}' file
With your given data:
$ awk '/Successfully done something. Email:/ && match($0, /Email: ([^ ]*) /, matches) {print matches[1]}' file
foo@bar.com
Related Topics
Maintain File and Folder Permissions Inside Archives
How to Connect to the Fitbit Zip Over Bluetooth 4.0 Le on Linux with Bluez
Renaming a Set of Files to 001, 002,
Sed to Insert on First Match Only
Can't Get Private Key with Openssl (No Start Line:Pem_Lib.C:703:Expecting: Any Private Key)
Run a Persistent Process via Ssh
Why Do I Get "Suspended (Tty Output)" in One Terminal But Not in Others
X86 Memory Access Segmentation Fault
Apache Mod_Rewrite Not Working with .Htaccess File
Replace First Few Lines with First Few Lines from Other File
Which Segments Are Affected by a Copy-On-Write
Sed - Piping a String Before the Last Line in a File
Strange File Permission in Docker Container (Question Marks on Permission Bit and User Bit)
How Are Threads/Processes Parked and Woken in Linux, Prior to Futex