LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2007, 07:30 AM   #1
Cyborg20
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
SED - The problem with the task


I ask you for the help.

I have the file tel.txt :

0523842156
0524566478
0783333333
.
.
0448888888

I have so to transform this text ( using Sed)

(052)384-21-56
(052)456-64-78
(078)333-33-33
.
.
(044)888-88-88


I will be grateful for the help
 
Old 11-23-2007, 07:55 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

sed 's/^\(...\)\(.*\)/(\1)\2/'infile

This sed command uses back referencing and can be broken up into 2 parts:

^\(...\) => First 3 chars at the beginning of the line
\(.*\) => the rest of the line

the \( and \) are special and point to parts that can be used into the replacement part (back referencing). In the replacement part you can use these with \1 and \2.

Hope this helps.
 
Old 11-23-2007, 08:55 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Code:
sed 's/\(...\)\(...\)\(..\)\(..\)/(\1)\2-\3-\4/' temp
(052)384-21-56
(052)456-64-78
(078)333-33-33
Maybe using the advanced regex option and a form like \(\[[:digit:]]{3}\) would be more concise, but just counting dot's seems easier to read.
 
Old 11-23-2007, 10:47 AM   #4
Cyborg20
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks a lot
 
Old 11-23-2007, 01:48 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Is it guaranteed that the area/city code is always three digits?


Cheers,
Tink
 
Old 11-24-2007, 06:10 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Tinkster brings up a good point. It is a good idea to be exact on the input set. If there are other patterns, you can have a separate sed command for each possible pattern.

You could run "egrep -v '^[[:digit:]]{10}$' tel.txt. If all of the numbers are 10 digits, there should be not output.

A phone number in the US would look like (701)555-5555. Having a simple textfile of numbers assumes that they all use the same format, and locale. Even if this is the case, what happens if one of the entries has an error. You might want to add a rule to reject it, or to write the entry to a separate file.

Code:
jschiwal@hpamd64:~> cat tel.sed
#n
/^[[:digit:]]\{10\}$/!w badnumber
/^[[:digit:]]\{10\}$/s/^\([[:digit:]]\{3\}\)\([[:digit:]]\{3\}\)\([[:digit:]]\{2\}\)\([[:digit:]]\{2\}\)/(\1)\2-\3-\4/p

jschiwal@hpamd64:~> cat numbers
0523842156
0524566478
0783333333
07755555555
jschiwal@hpamd64:~> rm badnumber
jschiwal@hpamd64:~> sed -f tel.sed numbers
(052)384-21-56
(052)456-64-78
(078)333-33-33
jschiwal@hpamd64:~> cat badnumber
07755555555
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Task bar no longer shows task Richard Rahl Linux - Newbie 3 04-12-2010 10:39 AM
gnome problem with task bars and screen resolution mattsoftnet Linux - Software 5 01-31-2006 10:10 AM
Problem with adding attendees to an assigned task in Evolution 2.4.0 on SUSE 10.0 dustybob Linux - Software 0 01-19-2006 02:47 PM
TRTP - Tricky Routing Task Problem tokehs Linux - Networking 7 08-27-2004 07:10 PM
init problem. task returns status7 dgoadby Linux - Newbie 0 09-18-2003 12:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration