Results 1 to 8 of 8

Thread: Need sed help

  1. #1
    Join Date
    Feb 2009
    Posts
    8

    Unanswered: 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!

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    This looks a lot like homework.
    What methods have you tried so far?
    Does it have to be done just in a sed command?

  3. #3
    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

  4. #4
    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.

  5. #5
    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.

  6. #6
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    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.

  7. #7
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  8. #8
    Join Date
    Feb 2009
    Posts
    8

    Cool

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •