sed one-liner to convert all uppercase to lowercase?

前端 未结 8 2055
慢半拍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 03:57

    Here are many solutions :

    To upercaser with perl, tr, sed and awk

    perl -ne 'print uc'
    perl -npe '$_=uc'
    perl -npe 'tr/[a-z]/[A-Z]/'
    perl -npe 'tr/a-z/A-Z/'
    tr '[a-z]' '[A-Z]'
    sed y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
    sed 's/\([a-z]\)/\U\1/g'
    sed 's/.*/\U&/'
    awk '{print toupper($0)}'
    

    To lowercase with perl, tr, sed and awk

    perl -ne 'print lc'
    perl -npe '$_=lc'
    perl -npe 'tr/[A-Z]/[a-z]/'
    perl -npe 'tr/A-Z/a-z/'
    tr '[A-Z]' '[a-z]'
    sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
    sed 's/\([A-Z]\)/\L\1/g'
    sed 's/.*/\L&/'
    awk '{print tolower($0)}'
    

    Complicated bash to lowercase :

    while read v;do v=${v//A/a};v=${v//B/b};v=${v//C/c};v=${v//D/d};v=${v//E/e};v=${v//F/f};v=${v//G/g};v=${v//H/h};v=${v//I/i};v=${v//J/j};v=${v//K/k};v=${v//L/l};v=${v//M/m};v=${v//N/n};v=${v//O/o};v=${v//P/p};v=${v//Q/q};v=${v//R/r};v=${v//S/s};v=${v//T/t};v=${v//U/u};v=${v//V/v};v=${v//W/w};v=${v//X/x};v=${v//Y/y};v=${v//Z/z};echo "$v";done
    

    Complicated bash to uppercase :

    while read v;do v=${v//a/A};v=${v//b/B};v=${v//c/C};v=${v//d/D};v=${v//e/E};v=${v//f/F};v=${v//g/G};v=${v//h/H};v=${v//i/I};v=${v//j/J};v=${v//k/K};v=${v//l/L};v=${v//m/M};v=${v//n/N};v=${v//o/O};v=${v//p/P};v=${v//q/Q};v=${v//r/R};v=${v//s/S};v=${v//t/T};v=${v//u/U};v=${v//v/V};v=${v//w/W};v=${v//x/X};v=${v//y/Y};v=${v//z/Z};echo "$v";done
    

    Simple bash to lowercase :

    while read v;do echo "${v,,}"; done
    

    Simple bash to uppercase :

    while read v;do echo "${v^^}"; done
    

    Note that ${v,} and ${v^} only change the first letter.

    You should use it that way :

    (while read v;do echo "${v,,}"; done) < input_file.txt > output_file.txt
    
    0 讨论(0)
  • 2020-11-28 03:59

    short, sweet and you don't even need redirection :-)

    perl -p -i -e 'tr/A-Z/a-z/' file
    
    0 讨论(0)
  • 2020-11-28 04:01

    With tr:

    # Converts upper to lower case 
    $ tr '[:upper:]' '[:lower:]' < input.txt > output.txt
    
    # Converts lower to upper case
    $ tr '[:lower:]' '[:upper:]' < input.txt > output.txt
    

    Or, sed on GNU (but not BSD or Mac as they don't support \L or \U):

    # Converts upper to lower case
    $ sed -e 's/\(.*\)/\L\1/' input.txt > output.txt
    
    # Converts lower to upper case
    $ sed -e 's/\(.*\)/\U\1/' input.txt > output.txt
     
    
    0 讨论(0)
  • 2020-11-28 04:02

    You also can do this very easily with awk, if you're willing to consider a different tool:

    echo "UPPER" | awk '{print tolower($0)}'
    
    0 讨论(0)
  • 2020-11-28 04:02

    I like some of the answers here, but there is a sed command that should do the trick on any platform:

    sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
    

    Anyway, it's easy to understand. And knowing about the y command can come in handy sometimes.

    0 讨论(0)
  • 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&/' <input >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.)

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