awktutorial/09-string-functions.awk

30 lines
821 B
Awk
Executable File

#!/usr/bin/awk -f
# https://www.grymoire.com/Unix/Awk.html
# awk supports a number of functions for working with strings too.
# index(), length(), split(), substr(), tolower(), toupper(), strtonum()
# and more: https://www.grymoire.com/Unix/Awk.html#toc-uh-40
BEGIN {
print "Type a sentence with 7+ words:"
getline sentence < "-"
if (sentence == "") {
print "You're no fun."
} else {
print "Now type one word that was in your sentence:"
getline searchstr < "-"
if (searchstr == "") {
print "You're no fun."
} else {
pos = index(sentence, searchstr)
if (pos > 0) {
print searchstr " found at position " pos "."
} else {
print searchstr " not found."
}
}
}
}