Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to sort some items in alphabetic order, but in a very specifc way.

I have, for example, the following list, each item separated by comma:

monkeys, big dogs, cats
pineapple, banana, orange
yellow, red, blue, green
silver, gold, platinum
delphi, java, c++, visual basic

An item here is defined by the piece of text: 1. starting in the beginning of a line and ended right before the first comma; 2. surrounded by commas and 3. starting right after the last comma and ending ate the end of line. So spaces are not separators, as in "big dogs" forms a single item.

I want to sort each line alphabetically, WITHOUT changing line order.

My desired result would be:

big dogs, cats, monkeys
banana, orange, pineapple
blue, green, red, yellow
gold, platinum, silver
c++, delphi, java, visual basic

My target list has got 3000+ lines, so it should be an automated process.


share|improve this question
What operating system are you using? – Daniel Beck Oct 19 '12 at 17:06
I'm using Windows 7, but I have cygwin installed also. – Daniel Oct 19 '12 at 17:06
Anybody can help me? – Daniel Oct 23 '12 at 21:46
Do you know any programming languages? Perl or Python could do this pretty easily. Perl would be my first choice, this being a text processing task. Any programming language could do it, though. Shouldn't take more than half an hour at very most. – Jack M Oct 23 '12 at 21:49
I code in java and delphi, but I feel python or perl would give me these results easily, wouldn't they? – Daniel Oct 23 '12 at 23:25
up vote 1 down vote accepted

Powershell one liner:

$sep=","; gc infile.txt |% {$line=($_ -split $sep)|% {$_.trim()}|sort;$line -join $sep} >outfile.txt

1. Uses PS 2 join syntax, which is more compact.
2. Using , as separator (as shown) will remove all leading/trailing spaces from words. That's is what I assume from context you want, but if I took your description literally, they should be retained. If you do wish so, remove |% {$_.trim()} (but then sort will not work 'as expected' for your example with leading spaces)
3. You may use ,<space> (or anything else, for that matter) as output separator (-join ", ") this will normalize any mixed input (with or without spaces after comma) to the one you selected.
4. Default encoding for output in PS is Unicode (UTF-16). You may change it by using | out-file -Encoding <encoding_type> instead of redirection > if you need to control that. To see available encodings, run help out-file -full

share|improve this answer
Thanks for the answer. Yeah, your right, I want to trim the input line, but not the output. Thus, the real separator is ",<space>". I changed to (-join ", ") and it worked. However I got another problem: I have some words containing characters such as "á", "é" and "ó" in the beginning. With this powershell command, such items are sorted before "a" alphabetically. It should be like this "a,á,b,c,d,e,é,f,g" ... etc. – Daniel Oct 24 '12 at 14:21
@Daniel I do not know under what regional settings you execute it, it worked for me for every single I tried. Anyway, you could use sort -culture to alter collation (eg. sort -culture en-us for English US). It may be also problem with input file encoding, but you would probably see 'garbage' as a result. – wmz Oct 24 '12 at 16:09
That's perfect! I set -culture -en-us and it works flawlessly! Thank you very much! – Daniel Oct 24 '12 at 17:19
@Daniel Glad it worked! I added a point about encoding just in case you need to set it to something different than default. – wmz Oct 24 '12 at 18:24

Here's one that ought to do it in python.

import csv

f = open("sortrows.csv", 'rb')
reader = csv.reader(f)

outf = open("sortedrows.csv", 'w')
for row in reader:
    outf.write(",".join(row) + "\n")

share|improve this answer
Won't the print(row) slow it down quite a bit? – Jack M Oct 24 '12 at 11:17
Thanks for the answer. This gives me a 0-byte-length "sortedrows.csv". – Daniel Oct 24 '12 at 14:14
What should I do? – Daniel Oct 24 '12 at 15:37
@JackM That's true. I left it in there from my debugging. – Ryan Oct 24 '12 at 19:08
It still gives a 0-byte sortedrows.csv when I run this script. I have python 3.3.0 installed. What's the problem? – Daniel Oct 24 '12 at 20:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .