AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Format phone book - SED - UNIX

Go down

Format phone book - SED - UNIX

Post by Lucaci Andrei on Tue Mar 20, 2012 1:20 am

Write a SED command that formats all the phone numbers in a file, such as (0744) 123-456.
The phone numbers in the file can appear in any of the following formats:
0744123456
0744 123456
0744 123 456
0744-123-456

The numbers are in a file phone. The modified version is in the file newphone.
The SED command for this problem:
sed 's/\([0-9]\{4\}\)[ -]\?\([0-9]\{3\}\)[ -]\?\([0-9]\{3\}\)/(\1) \2-\3/' phone>newphone


The SED command in general, for any format or punctuation:
sed 's/\([[:digit:]]\{4\}\)[[:space:]]\?[[:punct:]]\?\([[:digit:]]\{3\}\)[[:space:]]\?[[:punct:]]\?\([[:digit:]]\{3\}\)[[:space:]]\?[[:punct:]]\?/(\1) \2-\3/' phone>newphone


The most general approach of this SED command:
sed 's/\([[:digit:]]\{4\}\)[[:blank:]]\?[[:punct:]]\?\([[:digit:]]\{3\}\)[[:blank:]]\?[[:punct:]]\?\([[:digit:]]\{3\}\)[[:blank:]]\?[[:punct:]]\?/(\1) \2-\3/' phone>newphone

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 26
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum