Mike Sanders
2023-11-03 19:30:50 UTC
# tags: nth, ordinal, suffix, digit, numbers, awk, code
#
# appends ordinal suffix to space delimited numerals
# Michael Sanders 2023
# https://busybox.neocities.org/notes/nth.txt
#
# usage example: echo 101 42 23 98 foo | awk -f nth.txt
#
# output (1 per line): 101st 42nd 23rd 98th foo
#
# further reading:
# https://en.wikipedia.org/wiki/Ordinal_numeral
function nth(day) {
if (day ~ /^[0-9]+$/) {
if (day ~ /^1[1-3]$/ || day > 20) {
if (day % 10 == 1) return day "st"
if (day % 10 == 2) return day "nd"
if (day % 10 == 3) return day "rd"
}
return day "th"
}
return day
}
{
delete v
split($0, v)
for (x in v) print nth(v[x])
}
# eof
#
# appends ordinal suffix to space delimited numerals
# Michael Sanders 2023
# https://busybox.neocities.org/notes/nth.txt
#
# usage example: echo 101 42 23 98 foo | awk -f nth.txt
#
# output (1 per line): 101st 42nd 23rd 98th foo
#
# further reading:
# https://en.wikipedia.org/wiki/Ordinal_numeral
function nth(day) {
if (day ~ /^[0-9]+$/) {
if (day ~ /^1[1-3]$/ || day > 20) {
if (day % 10 == 1) return day "st"
if (day % 10 == 2) return day "nd"
if (day % 10 == 3) return day "rd"
}
return day "th"
}
return day
}
{
delete v
split($0, v)
for (x in v) print nth(v[x])
}
# eof
--
:wq
Mike Sanders
:wq
Mike Sanders