ass*_*sin 1158 string bash shell lowercase uppercase
在bash中有一种方法可以将字符串转换为小写字符串吗?
例如,如果我有:
a="Hi all"
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
"hi all"
Run Code Online (Sandbox Code Playgroud)
gho*_*g74 2028
各种方式:
$ echo "$a" | tr '[:upper:]' '[:lower:]'
hi all
Run Code Online (Sandbox Code Playgroud)
$ echo "$a" | awk '{print tolower($0)}'
hi all
Run Code Online (Sandbox Code Playgroud)
您可能会遇到以下示例遇到的可移植性问题:
$ echo "${a,,}"
hi all
Run Code Online (Sandbox Code Playgroud)
$ echo "$a" | sed -e 's/\(.*\)/\L\1/'
hi all
# this also works:
$ sed -e 's/\(.*\)/\L\1/' <<< "$a"
hi all
Run Code Online (Sandbox Code Playgroud)
$ echo "$a" | perl -ne 'print lc'
hi all
Run Code Online (Sandbox Code Playgroud)
lc(){
case "$1" in
[A-Z])
n=$(printf "%d" "'$1")
n=$((n+32))
printf \\$(printf "%o" "$n")
;;
*)
printf "%s" "$1"
;;
esac
}
word="I Love Bash"
for((i=0;i<${#word};i++))
do
ch="${word:$i:1}"
lc "$ch"
done
Run Code Online (Sandbox Code Playgroud)
Pau*_*ce. 415
在Bash 4中:
小写
$ string="A FEW WORDS"
$ echo "${string,}"
a FEW WORDS
$ echo "${string,,}"
a few words
$ echo "${string,,[AEIUO]}"
a FeW WoRDS
$ string="A Few Words"
$ declare -l string
$ string=$string; echo "$string"
a few words
Run Code Online (Sandbox Code Playgroud)
大写
$ string="a few words"
$ echo "${string^}"
A few words
$ echo "${string^^}"
A FEW WORDS
$ echo "${string^^[aeiou]}"
A fEw wOrds
$ string="A Few Words"
$ declare -u string
$ string=$string; echo "$string"
A FEW WORDS
Run Code Online (Sandbox Code Playgroud)
切换(未记录,但可选择在编译时配置)
$ string="A Few Words"
$ echo "${string~~}"
a fEW wORDS
$ string="A FEW WORDS"
$ echo "${string~}"
a FEW WORDS
$ string="a few words"
$ echo "${string~}"
A few words
Run Code Online (Sandbox Code Playgroud)
大写(未记录,但可选择在编译时配置)
$ string="a few words"
$ declare -c string
$ string=$string
$ echo "$string"
A few words
Run Code Online (Sandbox Code Playgroud)
标题案例:
$ string="a few words"
$ string=($string)
$ string="${string[@]^}"
$ echo "$string"
A Few Words
$ declare -c string
$ string=(a few words)
$ echo "${string[@]}"
A Few Words
$ string="a FeW WOrdS"
$ string=${string,,}
$ string=${string~}
$ echo "$string"
A few words
Run Code Online (Sandbox Code Playgroud)
要关闭declare
属性,请使用+
.例如,declare +c string
.这会影响后续分配,而不会影响当前值.
该declare
选项更改变量的属性,而不是内容.我的示例中的重新分配更新了内容以显示更改.
编辑:
${var~}
按照ghostdog74的建议添加"逐字逐句切换"().
编辑:更正了波形符合行为以匹配Bash 4.3.
shu*_*lov 120
echo "Hi All" | tr "[:upper:]" "[:lower:]"
Run Code Online (Sandbox Code Playgroud)
Ign*_*ams 76
a="$(tr [A-Z] [a-z] <<< "$a")"
Run Code Online (Sandbox Code Playgroud)
{ print tolower($0) }
Run Code Online (Sandbox Code Playgroud)
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
Run Code Online (Sandbox Code Playgroud)
net*_*tux 41
我知道这是一个古老的帖子,但我为另一个网站做了这个答案,所以我想我会在这里发布:
UPPER - > lower:使用python:
b=`echo "print '$a'.lower()" | python`
Run Code Online (Sandbox Code Playgroud)
或Ruby:
b=`echo "print '$a'.downcase" | ruby`
Run Code Online (Sandbox Code Playgroud)
或者Perl(可能是我最喜欢的):
b=`perl -e "print lc('$a');"`
Run Code Online (Sandbox Code Playgroud)
或PHP:
b=`php -r "print strtolower('$a');"`
Run Code Online (Sandbox Code Playgroud)
或者awk:
b=`echo "$a" | awk '{ print tolower($1) }'`
Run Code Online (Sandbox Code Playgroud)
或者Sed:
b=`echo "$a" | sed 's/./\L&/g'`
Run Code Online (Sandbox Code Playgroud)
或者Bash 4:
b=${a,,}
Run Code Online (Sandbox Code Playgroud)
或NodeJS,如果你有它(并且有点疯狂......):
b=`echo "console.log('$a'.toLowerCase());" | node`
Run Code Online (Sandbox Code Playgroud)
你也可以使用dd
(但我不会!):
b=`echo "$a" | dd conv=lcase 2> /dev/null`
Run Code Online (Sandbox Code Playgroud)
降低 - > UPPER:
使用python:
b=`echo "print '$a'.upper()" | python`
Run Code Online (Sandbox Code Playgroud)
或Ruby:
b=`echo "print '$a'.upcase" | ruby`
Run Code Online (Sandbox Code Playgroud)
或者Perl(可能是我最喜欢的):
b=`perl -e "print uc('$a');"`
Run Code Online (Sandbox Code Playgroud)
或PHP:
b=`php -r "print strtoupper('$a');"`
Run Code Online (Sandbox Code Playgroud)
或者awk:
b=`echo "$a" | awk '{ print toupper($1) }'`
Run Code Online (Sandbox Code Playgroud)
或者Sed:
b=`echo "$a" | sed 's/./\U&/g'`
Run Code Online (Sandbox Code Playgroud)
或者Bash 4:
b=${a^^}
Run Code Online (Sandbox Code Playgroud)
或NodeJS,如果你有它(并且有点疯狂......):
b=`echo "console.log('$a'.toUpperCase());" | node`
Run Code Online (Sandbox Code Playgroud)
你也可以使用dd
(但我不会!):
b=`echo "$a" | dd conv=ucase 2> /dev/null`
Run Code Online (Sandbox Code Playgroud)
当你说'shell'时,我假设你的意思,bash
但如果你可以使用zsh
它就像
b=$a:l
Run Code Online (Sandbox Code Playgroud)
小写和
b=$a:u
Run Code Online (Sandbox Code Playgroud)
对于大写.
Sco*_*ley 29
在zsh中:
echo $a:u
Run Code Online (Sandbox Code Playgroud)
得爱zsh!
dev*_*ull 18
使用GNU sed
:
sed 's/.*/\L&/'
Run Code Online (Sandbox Code Playgroud)
例:
$ foo="Some STRIng";
$ foo=$(echo "$foo" | sed 's/.*/\L&/')
$ echo "$foo"
some string
Run Code Online (Sandbox Code Playgroud)
Fin*_*sen 14
对于 Bash 命令行,根据区域设置和国际字母,这可能有效(根据其他人的答案汇总):
\n$ echo "ABC\xc3\x86\xc3\x98\xc3\x85" | python -c "print(open(0).read().lower())"\nabc\xc3\xa6\xc3\xb8\xc3\xa5\n$ echo "ABC\xc3\x86\xc3\x98\xc3\x85" | sed \'s/./\\L&/g\'\nabc\xc3\xa6\xc3\xb8\xc3\xa5\n$ export a="ABC\xc3\x86\xc3\x98\xc3\x85" | echo "${a,,}"\nabc\xc3\xa6\xc3\xb8\xc3\xa5\n
Run Code Online (Sandbox Code Playgroud)\n然而这些变体可能不起作用:
\n$ echo "ABC\xc3\x86\xc3\x98\xc3\x85" | tr "[:upper:]" "[:lower:]"\nabc\xc3\x86\xc3\x98\xc3\x85\n$ echo "ABC\xc3\x86\xc3\x98\xc3\x85" | awk \'{print tolower($1)}\'\nabc\xc3\x86\xc3\x98\xc3\x85\n$ echo "ABC\xc3\x86\xc3\x98\xc3\x85" | perl -ne \'print lc\'\nabc\xc3\x86\xc3\x98\xc3\x85\n$ echo \'ABC\xc3\x86\xc3\x98\xc3\x85\' | dd conv=lcase 2> /dev/null\nabc\xc3\x86\xc3\x98\xc3\x85\n
Run Code Online (Sandbox Code Playgroud)\n
Ale*_*Pes 14
[dev@localhost ~]$ TEST=STRESS2
[dev@localhost ~]$ echo ${TEST,,}
stress2
Run Code Online (Sandbox Code Playgroud)
fed*_*qui 13
Bash 5.1通过L
参数转换提供了一种直接的方法来做到这一点:
${var@L}
Run Code Online (Sandbox Code Playgroud)
例如,您可以说:
$ v="heLLo"
$ echo "${v@L}"
hello
Run Code Online (Sandbox Code Playgroud)
您还可以使用大写U
:
$ v="hello"
$ echo "${v@U}"
HELLO
Run Code Online (Sandbox Code Playgroud)
并将第一个字母大写u
:
$ v="hello"
$ echo "${v@u}"
Hello
Run Code Online (Sandbox Code Playgroud)
tec*_*rus 11
对于仅使用内置函数的标准shell(无bashisms):
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
lc(){ #usage: lc "SOME STRING" -> "some string"
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $uppers in
*$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
Run Code Online (Sandbox Code Playgroud)
对于大写:
uc(){ #usage: uc "some string" -> "SOME STRING"
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $lowers in
*$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
Run Code Online (Sandbox Code Playgroud)
haw*_*126 11
Pre Bash 4.0
Bash降低字符串的大小写并赋值给变量
VARIABLE=$(echo "$VARIABLE" | tr '[:upper:]' '[:lower:]')
echo "$VARIABLE"
Run Code Online (Sandbox Code Playgroud)
从bash
联机帮助页:
${参数^模式}
${参数^^模式}
${参数,模式}
${参数,,模式}
案例修改。此扩展修改参数中字母字符的大小写。该模式被扩展以产生一个模式,就像路径名扩展一样。参数扩展值中的每个字符都会根据模式进行测试,如果与模式匹配,则转换其大小写。该模式不应尝试匹配多个字符。^运算符将匹配模式的小写字母转换为大写;,运算符将匹配的大写字母转换为小写字母。^^和,, 扩展会转换扩展值中的每个匹配字符;^ 和,扩展仅匹配并转换扩展值中的第一个字符。如果省略模式,则将其视为 ? ,它匹配每个字符。如果参数是@或*,则大小写修改操作依次应用于每个位置参数,并且扩展是结果列表。如果 parameter是下标为@或*的数组变量,则依次对数组的每个成员进行大小写修改操作,并扩展为结果列表。
你可以试试这个
s="Hello World!"
echo $s # Hello World!
a=${s,,}
echo $a # hello world!
b=${s^^}
echo $b # HELLO WORLD!
Run Code Online (Sandbox Code Playgroud)
参考:http://wiki.workassis.com/shell-script-convert-text-to-lowercase-and-uppercase/
小智 7
我想赞扬我希望分享的命令,但实际情况是我从http://commandlinefu.com获取它以供我自己使用.它的优点是,如果您cd
到自己的主文件夹中的任何目录,它将递归地将所有文件和文件夹更改为小写,请谨慎使用.这是一个出色的命令行修复,对于存储在驱动器上的大量专辑特别有用.
find . -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
Run Code Online (Sandbox Code Playgroud)
您可以在find之后指定一个目录来代替点(.),表示当前目录或完整路径.
我希望这个解决方案证明是有用的,这个命令没有做的一件事是用下划线替换空格 - 哦,或许另一次.
转换大小写仅适用于字母。所以,这应该工作得很好。
我专注于将 az 之间的字母从大写转换为小写。任何其他字符都应该按原样打印在标准输出中......
将 az 范围内 path/to/file/filename 中的所有文本转换为 AZ
用于将小写字母转换为大写字母
cat path/to/file/filename | tr 'a-z' 'A-Z'
Run Code Online (Sandbox Code Playgroud)
用于从大写转换为小写
cat path/to/file/filename | tr 'A-Z' 'a-z'
Run Code Online (Sandbox Code Playgroud)
例如,
文档名称:
my name is xyz
Run Code Online (Sandbox Code Playgroud)
转换为:
MY NAME IS XYZ
Run Code Online (Sandbox Code Playgroud)
示例 2:
echo "my name is 123 karthik" | tr 'a-z' 'A-Z'
# Output:
# MY NAME IS 123 KARTHIK
Run Code Online (Sandbox Code Playgroud)
示例 3:
echo "my name is 123 &&^&& #@$#@%%& kAR2~thik" | tr 'a-z' 'A-Z'
# Output:
# MY NAME IS 123 &&^&& #@0@%%& KAR2~THIK
Run Code Online (Sandbox Code Playgroud)
对于 Bash3.2.+ | 苹果:
read -p 'What is your email? ' email
email=$(echo $email | tr '[:upper:]' '[:lower:]')
email="$email"
echo $email
Run Code Online (Sandbox Code Playgroud)
许多使用外部程序的答案,实际上并没有使用Bash
.
如果你知道你将有 Bash4 可用,你真的应该使用这个${VAR,,}
符号(它既简单又酷)。对于 4 之前的 Bash(例如,我的 Mac 仍然使用 Bash 3.2)。我使用了@ghostdog74 答案的更正版本来创建一个更便携的版本。
您可以调用lowercase 'my STRING'
并获得小写版本。我阅读了关于将结果设置为 var 的评论,但这并不是真正可移植的Bash
,因为我们不能返回字符串。打印它是最好的解决方案。使用类似var="$(lowercase $str)"
.
这是如何工作的
其工作方式是获取每个字符的 ASCII 整数表示,printf
然后使用adding 32
ifupper-to->lower
或subtracting 32
if lower-to->upper
。然后printf
再次使用将数字转换回字符。从'A' -to-> 'a'
我们有 32 个字符的差异。
使用printf
说明:
$ printf "%d\n" "'a"
97
$ printf "%d\n" "'A"
65
Run Code Online (Sandbox Code Playgroud)
97 - 65 = 32
这是带有示例的工作版本。
请注意代码中的注释,因为它们解释了很多内容:
#!/bin/bash
# lowerupper.sh
# Prints the lowercase version of a char
lowercaseChar(){
case "$1" in
[A-Z])
n=$(printf "%d" "'$1")
n=$((n+32))
printf \\$(printf "%o" "$n")
;;
*)
printf "%s" "$1"
;;
esac
}
# Prints the lowercase version of a sequence of strings
lowercase() {
word="$@"
for((i=0;i<${#word};i++)); do
ch="${word:$i:1}"
lowercaseChar "$ch"
done
}
# Prints the uppercase version of a char
uppercaseChar(){
case "$1" in
[a-z])
n=$(printf "%d" "'$1")
n=$((n-32))
printf \\$(printf "%o" "$n")
;;
*)
printf "%s" "$1"
;;
esac
}
# Prints the uppercase version of a sequence of strings
uppercase() {
word="$@"
for((i=0;i<${#word};i++)); do
ch="${word:$i:1}"
uppercaseChar "$ch"
done
}
# The functions will not add a new line, so use echo or
# append it if you want a new line after printing
# Printing stuff directly
lowercase "I AM the Walrus!"$'\n'
uppercase "I AM the Walrus!"$'\n'
echo "----------"
# Printing a var
str="A StRing WITH mixed sTUFF!"
lowercase "$str"$'\n'
uppercase "$str"$'\n'
echo "----------"
# Not quoting the var should also work,
# since we use "$@" inside the functions
lowercase $str$'\n'
uppercase $str$'\n'
echo "----------"
# Assigning to a var
myLowerVar="$(lowercase $str)"
myUpperVar="$(uppercase $str)"
echo "myLowerVar: $myLowerVar"
echo "myUpperVar: $myUpperVar"
echo "----------"
# You can even do stuff like
if [[ 'option 2' = "$(lowercase 'OPTION 2')" ]]; then
echo "Fine! All the same!"
else
echo "Ops! Not the same!"
fi
exit 0
Run Code Online (Sandbox Code Playgroud)
以及运行后的结果:
$ ./lowerupper.sh
i am the walrus!
I AM THE WALRUS!
----------
a string with mixed stuff!
A STRING WITH MIXED STUFF!
----------
a string with mixed stuff!
A STRING WITH MIXED STUFF!
----------
myLowerVar: a string with mixed stuff!
myUpperVar: A STRING WITH MIXED STUFF!
----------
Fine! All the same!
Run Code Online (Sandbox Code Playgroud)
这应该只适用于 ASCII 字符。
对我来说很好,因为我知道我只会将 ASCII 字符传递给它。
例如,我将它用于一些不区分大小写的 CLI 选项。
归档时间: |
|
查看次数: |
829855 次 |
最近记录: |