SunQuest
           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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old October 25th, 2004, 07:24 PM
FuzzyNips FuzzyNips is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 11 FuzzyNips User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sed Command Help (Introductory Level Question)

I have a file with the format:

John Adams 55
George Bull 77
Anne Blue 99

basically there is a first name then 1 or more spaces then the last name then 1 or more spaces and the age. I need to rewrite the file so the format looks like this:

Adams, John 55
Bull, George 77
Blue, Anne 99

last name, first name age (only one space between each).

I have to use the sed command and I'm trying to do it with the substituion option and I can't get it to work. It must be a one-line command and you can't use awk. The file name is sed1.data that I'm reading from and writing the new format to. Please help me, I appreciate it!

Reply With Quote
  #2  
Old October 25th, 2004, 08:01 PM
FuzzyNips FuzzyNips is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 11 FuzzyNips User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by FuzzyNips
I have a file with the format:

John Adams 55
George Bull 77
Anne Blue 99

basically there is a first name then 1 or more spaces then the last name then 1 or more spaces and the age. I need to rewrite the file so the format looks like this:

Adams, John 55
Bull, George 77
Blue, Anne 99

last name, first name age (only one space between each).

I have to use the sed command and I'm trying to do it with the substituion option and I can't get it to work. It must be a one-line command and you can't use awk. The file name is sed1.data that I'm reading from and writing the new format to. Please help me, I appreciate it!


I know I need to store the first name in tag#1, store the last name in tag#2 and the age in tag#3 then just send tag#2 add a comma and a space then tag #1 then another space and finally tag#3 back to the original file but everything I've tried hasn't worked.

Reply With Quote
  #3  
Old October 26th, 2004, 04:02 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,077 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 13 h 40 m 11 sec
Reputation Power: 8
replace 'S+T' by a litterally space followed by a tab character
this exactly match your exemple:
s/\([A-Z][a-z]*\)[S+T]*[S+T]\([A-Z][a-z]*\)[S+T]*[S+T]\([0-9][0-9]*\)/\2, \1 \3/
quick+dirty, you can use:
s/\(.*\)[S+T]*[S+T]\(.*\)[S+T]*[S+T]\(.*\)/\2, \1 \3/

Reply With Quote
  #4  
Old October 26th, 2004, 01:10 PM
FuzzyNips FuzzyNips is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 11 FuzzyNips User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks guggach. I was so close but I didn't have the space+tab in there to separate the 3 different fields, which explains why I was just taking the whole line. I have a similar question now that I'm trying to figure out on my own based on the help you gave me. This time I want to take only two fields and they are separated by spaces/tabs but also test.

Ex: (output of the who command)

z080069 pts/1 Sep 8 09:25 (euclid)

I need to take the first field (login ID) and the login time and separate them with a space. For the example above the output should look like:

z080069 09:25

Right now I'm doing this and getting the login ID and the time, but also the last field (which I don't want/need):

who | sed 's/^\([a-z0-9]*\)[S+T]*.*[S+T]*.*[S+T]*.*[S+T]*[S+T]\(..:..\)
/\1 \2/'

How do I go to the next line after getting the login time?

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Sed Command Help (Introductory Level Question)


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 6 hosted by Hostway