Command to print the phone numbers by making the area code(the first 3 digits of the phone numbers) prominent by surrounding them by parentheses

Viewed 201

Note There maybe some records in the file where the Name is missing and only the phone number is there or vice-versa.Such records should be considered as invalid records and should not be displayed in the output .Even should not prink blank line in the output for those records .

Sample input:

Danish 5555551212
3456782
Bulbul 5555551213
Kaloana 5555551214
Tina 6665551215
Cj
Mayuri 6665551216

output:

(555)5551212
(555)5551213
(555)5551214
(666)5551215
(666)5551216

MyCode:

BEGIN {FS=" ";c=0;}
{   
    if(NF>1)
    {
      s[c]=$2;
    c=c+1
    }
}

END{
  for (i=0;i<c;i++)
  {
    print s[i] 
  }

   

}

This is just 1/4th mile I have reached .

3 Answers

Could you please try following. Written and tested in https://ideone.com/ZMnuIp link with shown samples only.

awk '
NF==2 && match($2,/^[0-9]{3}/){
  print "(" substr($2,RSTART,RLENGTH) ")" substr($2,RSTART+RLENGTH)
}
' Input_file

Explanation: Checking condition if NFis 2 means if line has 2 fields AND 2nd field starts with 3 digits then do following. Printing ( then sub-string of 3 digits of 2nd field then printing ) and rest of the line then.

With GNU awk. Check with a regex (^[0-9]+$) if second column contains only numbers, print second column:

awk '$2~/^[0-9]+$/{print $2}' file

Output:

5555551212
5555551213
5555551214
6665551215
6665551216

Format second column with substr:

awk '$2~/^[0-9]+$/{print "(" substr($2,1,3) ")" substr($2,4)}' file

Output:

(555)5551212
(555)5551213
(555)5551214
(666)5551215
(666)5551216

This might work for you (GNU sed):

sed -nE 's/^\S+\s([0-9]{3})([0-9]+)$/(\1)\2/p' file

Turn off implicit printing -n.

On matching a valid phone number, surround the first 3 digits by parens and print the number only.


The solution consists of one substitution command, in the form s/LHS/RHS/ where RHS/LHS stands for right/left hand side.

The LHS is the regexp that matches something in the pattern space, the buffer where the current line is placed after its newline is removed.

The RHS is where the replacement of what is matched in the LHS is placed.

The /'s are the delimiters of the substitution command, usually the / is used but it can be nearly any other character e.g. #,: or %

The regex or regular expression consist of ^\S+\s([0-9]{3})([0-9]+)$

The ^ represents the place at the beginning of line and likewise the $ represents the place at the end of the line.

The \S represents a non-white space character it is the dual of \s which represents a white space character e.g. a space or tab.

The + represent one or more of the preceding character (or group), * is nearly same except it represent zero or more, ? represents optional (zero or one) and {3} represents exactly 3. So x{3} is the same as xxx.

The (...) represents a group where ... can be literal character(s), character classes, groups, alternations, back references or combinations of these. The group can be referred to later, either in the LHS or RHS of the substitution command, they are numbered \1 to \9, left to right, thus (abc)\1\1 represents abcabcabc.

The [0-9] is character class of digits here 0 thru 9.

So the LHS means: Starting from the beginning of the line is followed by one or more non-white spaces characters, followed by a white space character, followed by a grouping (\1) of 3 digits, followed by a grouping (\2) of one or more digits, followed by the end of the line.

The RHS represents: a literal (, followed by the first group i.e. the first 3 digits of the phone number, followed by a literal ), followed by the second group i.e. the remaining digits of the phone number.

If the substitution is successful, the result of the substitution is then printed to stdout because the p flag is present. Since the -n option is operating, that means only lines that match will be printed.

Related