[Klug-general] When it might be easier to translate

David Halliday david.halliday at gmail.com
Thu Sep 30 09:57:02 UTC 2010


I just had an interesting thing to solve. I had a comma separated list of
text strings that I wanted to sort alphabetically (I'm sure someone is going
to point out a better solution for this now).

The steps I thought up were quite simple.
Convert to a list (separate values by line breaks) in test.txt
Sort the list
Convert back to comma separated

My initial thought for the converting from comma separated values to a list
was sed (replace characters):
cat test.txt | sed 's/,/\
/g'

Not pretty to look at.

Pipe this into sort

then try and use sed to put it back.
cat test.txt | sed 's/,/\
/g' | sort | sed 's/\
/,/g'

Not pretty and it didn't work... A quick look up on google later and I'm
introduced to tr, translate (its an interesting and useful unix tool)
cat test.txt | tr ',' '\n' | sort | tr '\n' ','

While tr: http://unixhelp.ed.ac.uk/CGI/man-cgi?tr+1 has lots
of specific uses and this isn't exactly what it was designed for. The syntax
on an individual character replace is simpler.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20100930/e8ea468d/attachment.htm>


More information about the Kent mailing list