Last active
August 15, 2016 13:08
-
-
Save julcap/cfd7c5a0a8754bed96b18b531567c5b2 to your computer and use it in GitHub Desktop.
Shell script to validate numeric ID and email in CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The purpose of this script is to validate CSV file containing two columns, | |
# ID and email. ID is an integer, email can be quoted. | |
# It will catch inconsistent quotation marks, invalid emails and ID that is not numberic. | |
# Valid line examples: | |
# 1234567890;'email@address.com' | |
# 1234567890;"email@address.com" | |
# 1234567890;email@address.com | |
#!/usr/bin/env sh | |
input_file=$1 | |
line_number=0 | |
red='\e[31m' | |
green='\e[92m' | |
red_background='\e[41m' | |
blue_background='\e[44m' | |
end_color='\e[0m' | |
terminate=false | |
input_file=$1 | |
if [ ! $1 ];then echo "Please enter input file" && exit ; fi | |
for line in $(cat $input_file); do | |
let line_number+=1 | |
email="$(echo $line | awk -F ';' '{print $2}')" | |
cmn_authentication_id="$(echo $line | awk -F ';' '{print $1}')" | |
# Basic user id validation | |
if [ $(echo $cmn_authentication_id | egrep -v "^[0-9]+[0-9]$") ]; then | |
echo -e "Line $line_number: Invalid user id [$red_background $cmn_authentication_id $end_color]" | |
terminate=true | |
else | |
val_cmn_authentication_id=$green"OK"$end_color | |
fi | |
# Basic email validation | |
if [ $(echo $email | egrep "^('|\")?..+@..+\..+('|\")?$") ]; then | |
val_email=$green"OK"$end_color | |
else | |
echo -e "Line $line_number: Invalid email [$red_background $email $end_color]" | |
terminate=true | |
fi | |
if [ $(echo $email | egrep "^\"") ]; then | |
echo -en "." | |
if [ ! $(echo $email | egrep "\"$") ] || [ $(echo $email | egrep -o '"' | wc -l) -ne 2 ] || [ $(echo $email | egrep "'") ]; then | |
email="$red_background $email $end_color" | |
terminate=true | |
fi | |
fi | |
if [ $(echo $email | egrep "^'") ]; then | |
echo -en "." | |
if [ ! $(echo $email | egrep "'$") ] || [ $(echo $email | egrep -o "'" | wc -l) -ne 2 ] || [ $(echo $email | egrep "\"") ]; then | |
email="$red_background $email $end_color" | |
terminate=true | |
fi | |
fi | |
if [ $(echo $email | egrep -v "^('|\"){1}") ]; then | |
echo -en "." | |
if [ $(echo $email | egrep "('|\"){1}$") ]; then | |
echo -en "." | |
email="$red_background $email $end_color" | |
terminate=true | |
fi | |
fi | |
echo -e "$cmn_authentication_id $val_cmn_authentication_id => $email $val_email" | |
if [ $terminate = true ];then echo "Last line read: $line_number" && exit;fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment