Find the answer to your Linux question:
Results 1 to 5 of 5
Hey, is there an elegant way to sort the words within a line? e.g. if I have: bass dover carp albacore I want it to come out like so: albacore ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132

    Sorting Words in a Line


    Hey, is there an elegant way to sort the words within a line?
    e.g. if I have:
    bass dover carp albacore
    I want it to come out like so:
    albacore bass carp dover

    I could come up with a kludge, but it would be nice to know a real way to do it.

  2. #2
    Linux User
    Join Date
    Jun 2006
    Location
    Scotland
    Posts
    284
    H for Homework?

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Actually no, I am just a novice enthusiast that would like some help...

    I suppose you get a lot of questions here from people in school?

    Perhaps the reason my post looks like homework are the fish; I actually started with fruits and decided that I like fish better.

    I am writing a script that install a lot of packages with apt-get, and my OCD is telling me to sort them by category (already done) and by name within each category. Basically, I am trying to ease the pain of initial installation, and I want to be able to extend this script elegantly as I find more packages I like/need. Thus is the reason I want them in alphabetical order.

  4. #4
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Okay, for now I have a hack that works okay...
    Code:
    tr " " "\n" | sort | tr "\n" " "

  5. #5
    Just Joined!
    Join Date
    Feb 2009
    Posts
    9
    This is very simple to do in C++, and can be run through the terminal. Although, my knowledge in C++ is limited, so the only way I know to do it is an linked list, which might be over you're head if you don't know C++ already.

    It might help to know that you can use "<" or ">" with strings. ("bass" < "carp") will return true.

Posting Permissions

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