简体   繁体   English

sed 单行将所有大写转换为小写?

[英]sed one-liner to convert all uppercase to lowercase?

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 .我希望能够使用sed将文本文件中的所有内容转换为小写。 That means that the first sentence would then read, 'i have a textfile in which some words are printed in all caps.'这意味着第一句话将显示为“我有一个文本文件,其中一些单词全部大写。”

With tr :随着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 ):或者,在 GNU 上使用sed (但不支持 BSD 或 Mac,因为它们不支持\\L\\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
 

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:如果您有 GNU 扩展,则可以使用 sed 的 \\L (降低整个匹配,或直到达到 \\L [lower] 或 \\E [end - toggle case off]),如下所示:

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).作为旁注,GNU 扩展包括 \\U(上)、\\u(匹配的上一个字符)、\\l(匹配的下一个字符)。 For example, if you wanted to camelcase a sentence:例如,如果您想将句子驼峰化:

$ sed -E '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 use sequences such as \\w (match a word character) and the -E ( extended regex) option, which relieves you of having to escape the one-or-more quantifier ( + ) and certain other special regex characters.注意:由于假设我们有 GNU 扩展,我们可以使用诸如\\w (匹配单词字符)和-E扩展正则表达式)等序列,这样您就不必转义一个或多个量词( + ) 和某些其他特殊的正则表达式字符。

如果您愿意考虑使用不同的工具,您也可以使用awk轻松完成此操作:

echo "UPPER" | awk '{print tolower($0)}'

Here are many solutions :这里有很多解决方案:

To upercaser with perl, tr, sed and awk使用 perl、tr、sed 和 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、tr、sed 和 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 :复杂的 bash 到小写:

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 :复杂的 bash 到大写:

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 :简单的 bash 小写:

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

Simple bash to uppercase :简单的 bash 到大写:

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

Note that ${v,} and ${v^} only change the first letter.请注意,${v,} 和 ${v^} 仅更改第一个字母。

You should use it that way :你应该这样使用它:

(while read v;do echo "${v,,}"; done) < input_file.txt > output_file.txt
echo  "Hello  MY name is SUJIT "  | sed 's/./\L&/g'

Output:输出:

hello  my name is sujit

I like some of the answers here, but there is a sed command that should do the trick on any platform:我喜欢这里的一些答案,但是有一个 sed 命令应该可以在任何平台上解决问题:

sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'

Anyway, it's easy to understand.无论如何,这很容易理解。 And knowing about the y command can come in handy sometimes.了解 y 命令有时会派上用场。

If you are using posix sed如果您使用的是 posix sed

Selection for any case for a pattern (converting the searched pattern with this sed than use the converted pattern in you wanted command using regex:选择模式的任何情况(使用此 sed 转换搜索的模式,而不是使用正则表达式在您想要的命令中使用转换后的模式:

echo "${MyOrgPattern} | sed "s/[aA]/[aA]/g;s/[bB]/[bB]/g;s/[cC]/[cC]/g;s/[dD]/[dD]/g;s/[eE]/[eE]/g;s/[fF]/[fF]/g;s/[gG]/[gG]/g;s/[hH]/[hH]/g;s/[iI]/[iI]/g;s/[jJ]/[jJ]/g;s/[kK]/[kK]/g;s/[lL]/[lL]/g;s/[mM]/[mM]/g;s/[nN]/[nN]/g;s/[oO]/[oO]/g;s/[pP]/[pP]/g;s/[qQ]/[qQ]/g;s/[rR]/[rR]/g;s/[sS]/[sS]/g;s/[tT]/[tT]/g;s/[uU]/[uU]/g;s/[vV]/[vV]/g;s/[wW]/[wW]/g;s/[xX]/[xX]/g;s/[yY]/[yY]/g;s/[zZ]/[zZ]/g" | read -c MyNewPattern
 YourInputStreamCommand | egrep "${MyNewPattern}"

convert in lower case转换成小写

sed "s/[aA]/a/g;s/[bB]/b/g;s/[cC]/c/g;s/[dD]/d/g;s/[eE]/e/g;s/[fF]/f/g;s/[gG]/g/g;s/[hH]/h/g;s/[iI]/i/g;s/j/[jJ]/g;s/[kK]/k/g;s/[lL]/l/g;s/[mM]/m/g;s/[nN]/n/g;s/[oO]/o/g;s/[pP]/p/g;s/[qQ]/q/g;s/[rR]/r/g;s/[sS]/s/g;s/[tT]/t/g;s/[uU]/u/g;s/[vV]/v/g;s/[wW]/w/g;s/[xX]/x/g;s/[yY]/y/g;s/[zZ]/z/g"

same for uppercase replace lower letter between // by upper equivalent in the sed大写相同用sed中的大写替换//之间的小写字母

Have fun玩得开心

简短,甜蜜,你甚至不需要重定向:-)

perl -p -i -e 'tr/A-Z/a-z/' file

Instead of typing this long expression:而不是输入这个长表达式:

sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' input

One could use this:可以使用这个:

sed 'y/'$(printf "%s" {A..Z} "/" {a..z} )'/' input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM