upper- to lower-case using sed

前端 未结 7 1019
别那么骄傲
别那么骄傲 2020-12-05 07:21

I\'d like to change the following patterns:

getFoo_Bar

to:

getFoo_bar

(note the lower b)

Knowing

相关标签:
7条回答
  • 2020-12-05 07:55

    Somewhat shorter:

    echo getFoo_Bar | sed 's/_\(.\)/_\L\1/'
    
    0 讨论(0)
  • 2020-12-05 07:58

    To change getFoo_Bar to getFoo_bar using sed :

    echo "getFoo_Bar" | sed 's/^\(.\{7\}\)\(.\)\(.*\)$/\1\l\2\3/'
    

    The upper and lowercase letters are handled by :

    • \U Makes all text to the right uppercase.
    • \u makes only the first character to the right uppercase.
    • \L Makes all text to the right lowercase.
    • \l Makes only the first character to the right lower case. (Note its a lowercase letter L)

    The example is just one method for pattern matching, just based on modifying a single chunk of text. Using the example, getFoo_BAr transforms to getFoo_bAr, note the A was not altered.

    0 讨论(0)
  • 2020-12-05 08:04

    Shortest I can come up with:

    echo getFoo_Bar | sed 's/_./\L&/'
    
    0 讨论(0)
  • 2020-12-05 08:05
    echo getFoo_Bar | sed 's/[Bb]/\L&/g'
    
    0 讨论(0)
  • 2020-12-05 08:08

    If you want to write everything in lowercase just after underscore, than this would work for you:

    echo getFoo_Bar | gawk -F_ '{print tolower($2)}'
    
    0 讨论(0)
  • 2020-12-05 08:10

    You can use perl for this one:

    perl -pe 's/(get[A-Z][A-Za-z0-9]*)_([A-Z])/\1_\l\2/'
    

    The \l is the trick here.

    sed doesn't do upper/lowercase on matched groups.

    0 讨论(0)
提交回复
热议问题