Issue
I am trying to modify the phone number from a text file and display it the following ways using the Sed and Extended Regular Expression. I am new to Regular Expression so not sure what to do.
The first two numbers will be in parentheses and there are underscores after the second and sixth number. For example :(04)_1234_5678
The first number will become +61, followed by a space. For every three other digits, add a dash. For example +61 412-435-678
phone.txt
0412345678
0492669266
0458291324
0427193732
0429381912
0439201291
I have tried sed 'S/04/()/_1234_5678'
Solution
Using sed
$ sed s'/\(.\(.\)\)\(\(..\)\(..\)\)\(\(.\)\(...\)\)/(\1)_\3_\6\n+61 \2\4-\5\7-\8/' input_file
(04)_1234_5678
+61 412-345-678
(04)_9266_9266
+61 492-669-266
(04)_5829_1324
+61 458-291-324
(04)_2719_3732
+61 427-193-732
(04)_2938_1912
+61 429-381-912
(04)_3920_1291
+61 439-201-291
or individually
$ sed s'/.\(...\)\(...\)\(...\)/+61 \1-\2-\3/' input_file
+61 412-345-678
+61 492-669-266
+61 458-291-324
+61 427-193-732
+61 429-381-912
+61 439-201-291
$ sed s'/\(..\)\(....\)/(\1)_\2_/' input_file
(04)_1234_5678
(04)_9266_9266
(04)_5829_1324
(04)_2719_3732
(04)_2938_1912
(04)_3920_1291
Answered By - HatLess Answer Checked By - Clifford M. (WPSolving Volunteer)