sed one-liner to convert all uppercase to lowercase?

前端 未结 8 2176
慢半拍i
慢半拍i 2020-11-28 03:50

I have a textfile in which some words are printed in ALL CAPS. I want to be able to just convert everything in the textfile to lowercase, using sed. That means

8条回答
  •  野性不改
    2020-11-28 04:05

    If you have GNU extensions, you can use sed's \L (lower entire match, or until \L [lower] or \E [end - toggle casing off] is reached), like so:

    sed 's/.*/\L&/' output
    

    Note: '&' means the full match pattern.

    As a side note, GNU extensions include \U (upper), \u (upper next character of match), \l (lower next character of match). For example, if you wanted to camelcase a sentence:

    $ sed -r 's/\w+/\u&/g' <<< "Now is the time for all good men..." # Camel Case
    Now Is The Time For All Good Men...
    

    Note: Since the assumption is we have GNU extensions, we can also use the dash-r (extended regular expressions) option, which allows \w (word character) and relieves you of having to escape the capturing parenthesis and one-or-more quantifier (+). (Aside: \W [non-word], \s [whitespace], \S [non-whitespace] are also supported with dash-r, but \d [digit] and \D [non-digit] are not.)

提交回复
热议问题