Changing the case of a string with awk

前端 未结 4 1564
死守一世寂寞
死守一世寂寞 2021-02-20 15:55

I\'m an awk newbie, so please bear with me.

The goal is to change the case of a string such that the first letter of every word is uppercase and the remaining letters ar

4条回答
  •  长发绾君心
    2021-02-20 16:06

    When matching regex using the sub() function or others (like gsub() etc), it's best used in the following form:

    sub(/regex/, replacement, target)
    

    This is different from what you have:

    sub("regex", replacement, target)
    

    So your command becomes:

    awk '{ for (i=1;i<=NF;i++) sub(/\B\w+/, substr(tolower($i),2), $i) }1'
    

    Results:

    Abcd Efgh Ijkl Mnop
    

    This article on String Functions maybe worth a read. HTH.


    I should say that there are easier ways to accomplish what you want, for example using GNU sed:

    sed -r 's/\B\w+/\L&/g'
    

提交回复
热议问题