Revision 0285c814-8006-4c7d-b251-c46218bbd386 - Code Golf Stack Exchange
## Perl, 37 + 1 characters
s/1?\d\b/$&.((0,st,nd,rd)[$&]||th)/eg
This is a regexp substitution that appends the appropriate ordinal suffix to any numbers in `$_` that are not already followed by a letter. To apply it to file input, use the `p` command line switch, like this:
perl -pe 's/1?\d\b/$&.((0,st,nd,rd)[$&]||th)/eg'
This is a complete Perl program that reads input from stdin and writes the processed output to stdout. The actual code is 37 chars long, but the `p` switch [counts as one extra character](http://meta.codegolf.stackexchange.com/questions/273/on-interactive-answers-and-other-special-conditions).
### Sample input:
This is the 1 line of the sample input...
...and this is the 2.
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110
### Output:
This is the 1st line of the sample input...
...and this is the 2nd.
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th
101st 102nd 103rd 104th 105th 106th 107th 108th 109th 110th
Numbers already followed by letters will be ignored, so feeding the output again through the filter won't change it. Spaces, commas and periods between numbers are not treated specially, so they're assumed to separate numbers like any other punctuation. Thus, e.g. `3.14159` becomes `3rd.14159th`.