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>
-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,
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,
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
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
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!