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 .
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 NF
is 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.