Extract Emails From a Text File Using Grep Command

How To Extract Email Addresses From A Text File Using The Command Line

Hello guys, In this article, we will see how to extract email addresses from a text file using grep and sed commands. If the text file is too big and not in a pre-defined format, it’s too time-consuming to extract every email address manually. Using commands like grep and sed, email address or anything else can be extracted by their formats. For email addresses, the format is name@domain. If the text in a file is arranged in columns, We can use the awk command.

Extracting Email Addresses From a Text File Using the Grep Command

The grep command is used to print lines based on patterns. The syntax for grep command will be

grep -e -o <"pattern"> <filename>

where -e option is used to use patterns that are assigned and -o is used to print only the matched part. The expression for email addresses can be written as,

<username>@<domain>.<address>
[a-zA-Z0-9._]\+@[a-zA-Z]\+.[a-zA-Z]\+

Where a-z, A-Z, and 0-9 represent alphabets and numbers. In the first field, . and _ are given which are allowed for the username of an email address. the \+ indicates that the repeating alphabets and numbers should be considered.

Here, We have a text file named abc.txt as an example which contains some email addresses as shown below,

Screenshot From 2022 02 27 19 15 22

To extract the email addresses from the text file, Execute the following command:

grep -oe "[a-zA-Z0-9._]\+@[a-zA-Z]\+.[a-zA-Z]\+" abc.txt
Screenshot From 2022 02 27 19 20 02

If -o is not used, It will print the complete line where the email addresses are written. In the example above, It will print the output as shown below,

grep -e "[a-zA-Z0-9._]\+@[a-zA-Z]\+.[a-zA-Z]\+" abc.txt
Screenshot From 2022 02 27 19 33 40

Conclusion

So, we discussed how to extract email addresses from a text file using the grep command. Similarly, Other than email addresses, numbers or text in other formats can also be extracted by using the grep command. Thankyou for reading!