Results 1 to 4 of 4

Thread: Sort rows

  1. #1
    Join Date
    Jun 2004
    Posts
    6

    Unanswered: Sort rows

    I have a file like below:

    gina, crab.george, john, joe, crab.mckay

    Get rid of the 'crab.' and sort the rest of the output. I expect the output to be like:

    george, gina, joe, john, mckay

    Any help appreciated.

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    This smells like homework (and quite a cute test too)...

    Anyhow, here's one way. Use 'sed' to remove crab, the commas and to add a newline. You now have your input as a column, which 'sort' can sort. Then use awk to re-add the commas and transform the column back to a row.

    Code:
    echo "gina, crab.george, john, joe, crab.mckay" | sed 's/crab\.//g;s/, */\
    /g' | sort | awk '{printf (comma"%s",$0); comma=","}'
    Damian

  3. #3
    Join Date
    Jun 2004
    Posts
    6
    Hi Damian,

    Oops sorry but this fails.

    When I execute:

    echo "gina, crab.george, john, joe, crab.mckay" | sed 's/crab\.//g;s/, */\/g' | sort | awk '{printf (comma"%s",$0); comma=","}'

    It says:

    sed: command garbled: s/crab\.//g;s/, */

    Am I missing out on something or doing something wrong.

    Yahoo

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    No, you ARE doing something wrong...

    The newline that I included in the sed command (after the '\') is necessary.

Posting Permissions

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