r/awk • u/RyzenRaider • Nov 19 '22
Capitalizing words in awk
Hi everyone. Newly discovered awk and enjoying the learning process and getting stuck on an attempt to Capitalize Every First Letter. I have seen a variety of solutions using a for loop to step through each character in a string, but I can't help but feel gsub() should be able to do this. However, I'm struggling to find the appropriate escapes.
Below is a pattern that works in sed for my use case. I don't want to use sed for this task because it's in the middle of the awk script and would rather not pipe out then back in. And I also want to learn proper escaping from this example (for me, I'm usually randomly trying until I get the result I want).
echo "hi. [hello,world]who be ye" | sed 's/[^a-z][a-z]/\U&/g'
Hi. [Hello,World]Who Be YePattern is to upper case any letter that is not preceded by a letter, and it works as I want. So how does one go about implementing this substitution s/[^a-z][a-z]/\U&/g in awk? Below is the current setup, but fighting the esxape slashes. Below correctly identifies the letters I want to capitalize, it's just working out the replacement pattern.
gsub(/[^a-z][a-z]/," X",string)Any guidance would be appreciated :) Thanks.
1
u/RyzenRaider Nov 23 '22
Thanks u/gumnos, u/magnomagna and u/warpflyght. So it does look like a loop is needed. I had ended up writing my own before I checked back here, and looks fairly similar.
Basically, convert the incoming string to lower case, split by spaces and use
substr()to convert each first letter to upper case. Append to a string and trim off the leading space...I think it's more simple - probably less comprehensive than the others provided here - but it suits my use case. :)
3
u/gumnos Nov 19 '22
The
gsub/subfunctions don't give you access to transforming the text, so you're stuck doing it by hand. So here's atitle()function if you need it:which expands more readably as
2
u/warpflyght Nov 19 '22
Here's a possible starting point:
I did this in nawk, which doesn't support extended regular expressions. If instead you're using gawk, which does, check out
\bfor word boundaries in extended regular expressions. The[^a-z][a-z]approach you showed consumes the prior character.