Friday, April 1, 2016

Bash Shell Script Exact Matching Portion of Email Address Input with HEAD TAIL and GREP Email Address Pattern


What the code does:

The code below uses GREP to match specific pattern within the text file. Here using the "head" command it gets the first 10 lines. Those 10 lines are the input for "tail" command which in turn get the last 6 lines from those 10 lines input. So the lines we get are 5, 6, 7, 8, 9, 10 in the original file. Now these 6 lines are fed into Grep using pipe to find match using the given regex.

Bash Script Code:

Save the code below in a file with extension ".sh". For example, SpecificLinesEmailGrepRegex.sh
#!/bin/bash
head -10 "EmailAddress.txt" | tail -6 | grep --color -E -w "^[A-Za-z0-9._]+@[A-Za-z0-9.]+\.[A-Za-z]{2,6}"

Input File:

Save the text below in a file named "EmailAddress.txt"
Valid Email addresses:
google@gmail.com
afsjk_wegkj@yahoo.com
asdsd/1221@asd.asd.sd.rtg.erer.erer
abcd@cse.uiu.ac.bd
Invalid email addresses:
asdgh@asd@gmail.com reason: number of @ is more than one
hello@asdsd reason: no .domain name
hasan@.com reason: At least one charecter needed between @ and .

Running the Code:

Before doing anything make sure the text file and the script file are located in the same directory. In order to run the code just type the code below if you named your script file as "SpecificLinesEmailGrepRegex.sh".

bash SpecificLinesEmailGrepRegex.sh

No comments: