| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

02-22-09, 23:36
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 8
|
|
|
Need sed help
|
|
Hi All...I need to rearrange columns using a one-line sed command. Here's my file:
John Adams 55
George Bull 77
Anne Blue 99
Janet Blue 67
Ben Benjamin 78
Ted White 32
The spaces between columns are random. I need the file to look like this:
Adams, John 55
Bull, George 77
etc...
Basically, last name, first name and age. separated by no more than one space. Thanks in advance for your help!
|
|

02-23-09, 04:49
|
|
vaguely human
|
|
Join Date: Jun 2007
Location: London
Posts: 2,517
|
|
This looks a lot like homework.
What methods have you tried so far?
Does it have to be done just in a sed command?
|
|

02-23-09, 22:26
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 8
|
|
|
|
Thanks for the quick reply...I'm a beginner in UNIX, and it is in fact homework, but unfortunately, it's not like anything covered in the text, and the instructor hasn't been responsive (it's a distance learning class). The fact I haven't come up with a solution certainly isn't due to a lack of effort.
A one-line sed command is required (seems like it would be a pretty extreme command), the closest I've been able to come is keeping everything in place, including the random spaces between columns, but with the comma after the age.
These are some of the commands I've tried...
sed 's/.* $/&, [0-9][0-9]/' filename
sed 's/^ .* $/&, [0-9][0-9]/' filename
|
|

02-23-09, 22:30
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 8
|
|
I'm not necessarily looking for the answer, per se, but if you can point me in the right direction or just a hint that would be great, if it's possible. Ultimately, I really want the knowledge, not to just get through the class. Thanks again.
|
|

02-23-09, 22:32
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 8
|
|
One more thing..I've also tried using back references, but still don't have a solid grasp on those and get only 'invalid back reference' errors. 
|
|

02-24-09, 03:42
|
|
Registered User
|
|
Join Date: May 2005
Location: South Africa
Posts: 1,258
|
|
Code:
sed 's/\([^ ]\{1,\}\) \(.*\) \([0-9]\{1,\}\)/\2, \1 \3/' filename
This will work with multi word last names e.g. Piet Van Der Hoff 103
sed supports the basic regular expressions described on the regexp(5) manual page. Read the man page.
man -s 5 regexp
|
Last edited by pdreyer; 02-24-09 at 03:46.
|

02-24-09, 05:09
|
|
vaguely human
|
|
Join Date: Jun 2007
Location: London
Posts: 2,517
|
|
Funny thing is I always thought I was quite good with sed etc but in reality I just get by with a moderate knowledge of regular expressions and then just use the 's/fred/john/' type commands. I certainly had no clue what the \{1,\} bit does in pdreyers example does! I'm not sure whether his example will work with multiple spaces etc though.
My attempt might be :
Code:
sed 's/\([^ ]*\) *\([^ ]*\) *\([0-9]*\)/\2, \1 \3/' filename
Explanation - [^ ]* matches as many non space characters as it can find
- * matches as many space chars as it can find
- \( \) takes whatever pattern that is matched within the brackets and stores them for later use
- \2, \1 \3 uses the patterns stored ie show 2nd pattern followed by a comma then the 1st then the 3rd
The man pages are ok if you know what you're looking for but are terrible to learn from. There are good books ie "Unix in a nutshell" or "Unix Programming Environment" by Kernighan. The web is also a good source of knowledge ie this page.
Mike
|
|

02-25-09, 20:12
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 8
|
|
Thanks for all your help, guys! Mike, your solution worked! Here are the results of your commands:
Pdreyer:
Adams , John 55
Bull , George 77
Blue , Anne 99
Blue , Janet 67
Benjamin , Ben 78
White , Ted 32
Mike:
Adams, John 55
Bull, George 77
Blue, Anne 99
Blue, Janet 67
Benjamin, Ben 78
White, Ted 32
Pdreyer, thanks for pointing me in the right direction. It won't show in the thread, but fyi, your command didn't eliminate the extra spaces between the last name and comma. I'm pretty sure I'll be able to go through it and see what needs to be changed.
Mike, yours actually provided the correct solution. I'll be able to work through it to see how it actually works.
Needless to say, I appreciate the time you've donated to my cause.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|