Hi:
I had need in an awk script (solaris 7) to determine if a field was a social security number, SSN. In Dale Douugherty's book Sed & Awk, he describes a sed regular expression for SSN. The following sed stub substitutes correctly if the data echoed to sed is 3 numerics followed by dash, 2 numerics followed by dash, and 4 numerics:
echo "111-11-1111"| sed 's/^[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}$/222-22-2222/g'
I tried the same regex with awk/nawk:
cnt=`echo "111-11-1111" |awk ' {
if($1 ~ /^[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}$/)
print 1
else print 0 } '`
echo $cnt # 0 if not SSN and 1 if is
And it FAILS. I don't get an error, but neither does it work.
I tried the same regex with awk not using the meta characters \) and \(, and it works:
cnt=`echo "111-11-1111" |awk ' {
if($1 ~ /^[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$/ )
print 1
else print 0 } '`
echo $cnt # 0 if not SSN and 1 if is
Does anyone know why \( and \) do not work? Am I missing something?
Regards,
Ed
I had need in an awk script (solaris 7) to determine if a field was a social security number, SSN. In Dale Douugherty's book Sed & Awk, he describes a sed regular expression for SSN. The following sed stub substitutes correctly if the data echoed to sed is 3 numerics followed by dash, 2 numerics followed by dash, and 4 numerics:
echo "111-11-1111"| sed 's/^[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}$/222-22-2222/g'
I tried the same regex with awk/nawk:
cnt=`echo "111-11-1111" |awk ' {
if($1 ~ /^[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}$/)
print 1
else print 0 } '`
echo $cnt # 0 if not SSN and 1 if is
And it FAILS. I don't get an error, but neither does it work.
I tried the same regex with awk not using the meta characters \) and \(, and it works:
cnt=`echo "111-11-1111" |awk ' {
if($1 ~ /^[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]$/ )
print 1
else print 0 } '`
echo $cnt # 0 if not SSN and 1 if is
Does anyone know why \( and \) do not work? Am I missing something?
Regards,
Ed