views:

110

answers:

4

I want to test whether a phone number is valid, and then translate it to a different format using a script. This far I can test the number like this:

sed -n -e '/(0..)-...\s..../p' -e '/(0..)-...-..../p'

However, I don't just want to test the number and output it, I would like to remove the brackets, dashes and spaces and output that.

Is there any way to do that using sed? Or should I be using something else, like AWK?

A: 

this suggestion depends on how your number format looks like , for example, i assume phone number like this

echo "(703) 234 5678" | awk '
{
 for(i=1;i<=NF;i++){
    gsub(/\(|\)/,"",$i)    # remove ( and )
    if ($i+0>=0  ){ # check if it more than 0 and a number
        print $i
    }
    if (){
       # some other checks
    }
 }
}
'

do it systematically, and you don't have to waste time crafting out complex regex

ghostdog74
+2  A: 

I'm not sure why you're using a 0 in that position. You're saying "a zero followed by any two characters" in the area code position. Is that really what you mean?

Anyway, you want to use the sed substitution operator with the p command in conjunction with the -n switch. Here's one way to do it:

sed -n 's/(\([0-9][0-9][0-9]\))\s\?\([0-9][0-9][0-9]\)[- ]\([0-9][0-9][0-9][0-9]\)/\1\2\3/p'
Jonathan Feinberg
Yes, area code is South African :P I was just using any character for testing
pypmannetjies
Thanks a bunch. With some tweaking for the specific format it worked perfectly.
pypmannetjies
Sweet. I was "flying by instruments", so I'm glad it worked. :)
Jonathan Feinberg
I had to escape the question mark on your BRE version. Did it work for you without?
Dennis Williamson
+1  A: 

You can also use something as simple as egrep to validate lines and tr to remove the characters you don't want to see:

egrep "\([0-9]+\)[0-9.-]+" <file> |tr -d '()\-'

Note that it will only work if you don't want to keep any of those characters.

RedGlyph
+1  A: 

This is a more succinct version of Jonathan Feinberg's answer. It uses extended regular expressions to avoid having to do all the escaping that the curly braces would require (in addition to moving the escaping of parentheses from the special ones to the literal ones).

sed -r 's/\(([[:digit:]]{3})\)\s?([[:digit:]]{3})[ -]([[:digit:]]{4})/\1\2\3/'
Dennis Williamson
Nice. Much more elegant and good to know.
pypmannetjies