Sed and posix awk solution


  • 0
    W

    Sed is needed to catch multiple white space issues

    awk --posix allows awk to use {n} notation to match exactly n counts of a regex

    Read from the file file.txt and output all valid phone numbers to stdout.

    cat file.txt | sed 's/ +/x/g' | awk --posix '{if (NF==2) {if ($1 ~ /^([0-9]{3})/ && $2 ~ /[0-9]{3}-[0-9]{4}$/) print $0} else { if (NF==1 && $1 ~ /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/) {print $0} } }'


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.