UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old October 5th, 2004, 09:26 AM
miry2j miry2j is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 37 miry2j User rank is Private First Class (20 - 50 Reputation Level)miry2j User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 h 35 m 18 sec
Reputation Power: 4
sed problem

I am a newbie in unix scripting so sorry if this is a dumb question. I am trying to reverse the order of first and last names if a file using sed. Here is a sample line:
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

Please help have no idea how to start this.

Reply With Quote
  #2  
Old October 5th, 2004, 10:14 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
and the output should be ??????

Reply With Quote
  #3  
Old October 5th, 2004, 10:46 AM
miry2j miry2j is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 37 miry2j User rank is Private First Class (20 - 50 Reputation Level)miry2j User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 h 35 m 18 sec
Reputation Power: 4
Sorry the out put should be the first and last name reversed everything else stays the same.

Reply With Quote
  #4  
Old October 5th, 2004, 11:22 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
Code:
cat /var/tmp/asb.txt
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

sed -e 's/ /+/' /var/tmp/asb.txt                                        
Tommy+Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

sed -e 's/ /+/' -e 's/:/+/' /var/tmp/asb.txt
Tommy+Savage+408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

sed -e 's/ /+/' -e 's/:/+/' /var/tmp/asb.txt |awk -F"+" '{print $2" "$1":"$3}'
Savage Tommy:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200


HTH

Reply With Quote
  #5  
Old October 5th, 2004, 12:53 PM
miry2j miry2j is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 37 miry2j User rank is Private First Class (20 - 50 Reputation Level)miry2j User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 h 35 m 18 sec
Reputation Power: 4
Sorry I should have specified it should be just one sed command. No using awk.

Reply With Quote
  #6  
Old October 6th, 2004, 07:11 AM
Muthukumar_K Muthukumar_K is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 Muthukumar_K User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Muthukumar_K
To use sed we have to pattern exactness there. We have to change the pattern with replacement there.

In your example,
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

we knew Tommy Savage, Oxbow Court are to be changed there. But relative pattern to idenitfy them are not there. For example, if we have + symbol there then we can do sed there with identification of + there. But the symbol + must not be with other strings there.

So simply to do this,

sed "s/Tommy Savage/Savage Tommy/g;s/Oxbow Court/Court Oxbow/g" <inputfile>

HTH.

Reply With Quote
  #7  
Old October 9th, 2004, 10:53 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
miry2j
if you try to change:

aaa bbb:ccccc and more
to
bbb aaa:ccccc and more
then note you have 2 points
the space between 'b a' and the : between 'a:c'
IF THEY ARE CONSTANT, the way in sed is:
s/^\(.*\)\( [ ]*\)\(.*\):\(.*\)/\3\2\1:\4/
NOTE the spaces.

Muthukumar_K: try to be a little more abstract

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > sed problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway