Archive for December, 2013

Interesting – Useful Python Functions

December 20, 2013 Leave a comment
Categories: Interesting

Interesting – Understanding Imports and Pythonpath

December 4, 2013 Leave a comment
Categories: Interesting, python

sed – Convert to Title Case

December 2, 2013 3 comments

I needed to ensure that text read from a file was in Title Case. Solution had to be sed 🙂

1. This is what I came up with, then I googled and found much simpler solutions that work.

echo "noRTh weST, 0" |sed 's/^\([A-Za-z]\)\([A-Za-z]\+\)/\U\1\L\2/'
North weST, 0

2. A simpler solution

echo "noRTh weST, 0" | sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g'
North West, 0

sed -e 's/.*/\L&/' - Convert all the characters to lowercase
sed -e 's/[a-z]*/\u&/g' -
Convert only the first character or each word to uppercase.

\L – Turn the replacement to lowercase until a `\U’ or `\E’ is found,
& – References the whole matched portion of the pattern space.
\u – Turn the next character to uppercase

3. Above solution will fail for the following cases

echo "nor'th we'ST, 0" | sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g'
Nor'Th We'St, 0

echo "nor'th we'ST, 0" | sed -e 's/.*/\L&/' -e 's/[[:graph:]]*/\u&/g'
Nor'th We'st, 0

/[[:graph:]]/ – Non-blank character (excludes spaces, control characters, and similar)


Categories: sed