Posted by Derek@TheDailyLinux »
2 Comments »
You can validate an email address using POSIX Regular Expressions like the following examples:
^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com|edu|gov]+$
This is two different ways of doing the same thing. If you want tighter control on your validity, I would recommend specifying com, edu, gov or other domain names you consider valid like shown in the second example above.
A quick PHP script to demonstrate:
<?php
// hit this in a browser and specify the email in the URL:
// http://www.mywebsite.com/email_validate.php?email=foo@bar.com
$email=$_GET['email'];
//if (!eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $email)) {
if (!eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com|edu|gov]+$', $email)) {
echo "<p>That is not a valid email address.</p>".
"<p>Please return to the previous page and try again.</p>";
exit;
}
else
echo "<p>That is one sexy email address!</p>";
?>
Posted by Derek@TheDailyLinux »
Add Comment »
If you’re trying to scrub your input of a script, a good way to do it is using POSIX along with the tr -d command. This is demonstrated in the shell script below.
#!/bin/sh
# Any of the following can be used, only a few are
# demonstrated:
#[:alnum:] # Alphanumeric characters
#[:alpha:] # Alphabetic characters
#[:lower:] # Lowercase letters
#[:upper:] # Uppercase letters
#[:digit:] # Decimal digits
#[:xdigit:] # Hexadecimal digits
#[:punct:] # Punctuation
#[:blank:] # Tabs and spaces
#[:space:] # Whitespace characters
#[:cntrl:] # Control characters
#[:print:] # All printable characters
#[:graph:] # All printable characters except for space
#[a-zA-Z0-9] # Same as [:alnum:]. POSIX can be used.
if [ $# -lt 1 ]; then
echo "Usage: $0 <text>"
exit
fi
text=$1
# Check that input is only numeric
if [ -z `echo $text | tr -d "[:digit:]"` ]; then
echo "Input contains only numeric characters"
else
echo "Error: input contains non-numeric characters"
fi
# Check that input is only alpha
if [ -z `echo $text | tr -d "[:alpha:]"` ]; then
echo "Input contains alpha characters"
else
echo "Error: input contains non-alpha characters"
fi
# Check that input is only alphanumeric
if [ -z `echo $text | tr -d "[:alnum:]"` ]; then
echo "Input contains alphanumeric characters"
else
echo "Error: input contains non-alphanumeric characters"
fi
Let’s give it a test run, shall we?
user@localhost$ ./input.sh text
Error: input contains non-numeric characters
Input contains alpha characters
Input contains alphanumeric characters
user@localhost$ ./input.sh text123
Error: input contains non-numeric characters
Error: input contains non-alpha characters
Input contains alphanumeric characters
user@localhost$ ./input.sh 123
Input contains only numeric characters
Error: input contains non-alpha characters
Input contains alphanumeric characters
user@localhost$
Posted by Derek@TheDailyLinux »
4 Comments »
Here’s a cute trick to change or insert a line in a file given a line number using sed. You can pair it with grep to make it even more powerful with pattern matching.
#!/bin/sh
if [ $# -lt 1 ]; then
echo "Usage: $0 <filename>"
exit
fi
# Variables
#file=testfile.txt
file=$1
# First, change line number $linenum, then insert
# two spaces at the beginning of that same line
linenum=1
sed -i "${linenum}c This line was changed" $file
sed -i "${linenum}s/^/ /" $file
# Insert a line at $linenum, then insert text at
# the end of that same line.
linenum=2
sed -i "${linenum}i This line was inserted" $file
sed -i "${linenum}s/$/ end of line/" $file
# Match a particular pattern in the file and insert a line
# directly above the first match (will need massaging for
# multiple lines, perhaps using an array)
linenum=`grep -n pattern $file | head -n1 | cut -d: -f1`
linenum_above=`echo "$linenum - 1" | bc`
sed -i "${linenum_above}i This line was inserted" $file
echo "Resulting File:"
cat $file
As usual, take a look at the man pages for sed for more information.