菜鸟笔记
提升您的技术认知

linux 中大小写转换

1、创建测试数据

[root@linuxprobe test3]# cat a.txt
e i j
s e f
Y U D
S D G

 

2、小写转换为大写 tr

[root@linuxprobe test3]# tr [a-z] [A-Z] < a.txt ## 所有小写字符转换为大写 E I J S E F Y U D S D G
[root@linuxprobe test3]# tr [A-Z] [a-z] < a.txt ## 所有的大写字符转换为小写字符 e i j s e f y u d s d g
[root@linuxprobe test3]# tr [A-Z][a-z] [a-z][A-Z] < a.txt ## 大小写字符互换 E I J S E F y u d s d g
[root@linuxprobe test]# cat a.txt | tr [:upper:] [:lower:] e i j s e f y u d s d g [root@linuxprobe test]# cat a.txt | tr [:lower:] [:upper:] E I J S E F Y U D S D G

3、 sed

sed.1

[root@linuxprobe test3]# sed 's/[a-z]/\U&/g' a.txt ##小写字母转换为大写字母 E I J S E F Y U D S D G
[root@linuxprobe test3]# sed 's/^[a-z]/\U&/g' a.txt ## 第一个小写字母转换为大写 E i j S e f Y U D S D G

[root@linuxprobe test3]# sed 's/^[A-Z]/\l&/g' a.txt ## 第一个大写字母转换为小写字母 e i j s e f y U D s D G
[root@linuxprobe test3]# sed 's/[A-Z]/\l&/g' a.txt ## 所有大写字符转换为小写字母
e i j
s e f
y u d
s d g
 

 

sed.2实现将首字母转换为大写

[root@linuxprobe test]# cat a.txt ##测试数据 asfe geri fsddj dsfs gfde gfdff fGgd gdsU hgfgh Sgdf fgdD dfgG [root@linuxprobe test]# sed "s/\b\(.\)/\u\1/g" a.txt ##实现所有首字母大写 Asfe Geri Fsddj Dsfs Gfde Gfdff FGgd GdsU Hgfgh Sgdf FgdD DfgG [root@linuxprobe test]# sed "s/\b\(.\)/\U\1/g" a.txt ## 实现所有首字母大写 Asfe Geri Fsddj Dsfs Gfde Gfdff FGgd GdsU Hgfgh Sgdf FgdD DfgG [root@linuxprobe test]# sed "s/\b\(.\)/\U\1/" a.txt ## 实现第一个首字母大写 Asfe geri fsddj Dsfs gfde gfdff FGgd gdsU hgfgh Sgdf fgdD dfgG

 

sed.3 确保只有首字母是大写:

[root@linuxprobe test]# cat a.txt asfe geri fsddj dsfs gfde gfdff fGgd gdsU hgfgh Sgdf fgdD dfgG [root@linuxprobe test]# sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g' a.txt Asfe Geri Fsddj Dsfs Gfde Gfdff Fggd Gdsu Hgfgh Sgdf Fgdd Dfgg
[root@linuxprobe test]# cat b.txt RDFS GFSDG DFDS fsFS dfGGF rewr FGDF GFDHG FSD [root@linuxprobe test]# sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g' b.txt Rdfs Gfsdg Dfds Fsfs Dfggf Rewr Fgdf Gfdhg Fsd

 

 

 

 

4、 awk

[root@linuxprobe test3]#  awk '{print toupper($0)}' a.txt ## 转换为大写 E I J S E F Y U D S D G [root@linuxprobe test3]# awk '{print toupper($1)}' a.txt ## 第一列转换为大写 E S Y S
[root@linuxprobe test3]#  awk '{print tolower($0)}' a.txt ## 转换为小写 e i j s e f y u d s d g [root@linuxprobe test3]# awk '{print tolower($1)}' a.txt ##第一列转换为小写 e s y s

 

5、python确保只有首字母是大写 

[root@linuxprobe test]# echo -n "design & engineering" | python3 -c "import sys; print(sys.stdin.read().title())" Design & Engineering [root@linuxprobe test]# cat a.txt asfe geri fsddj dsfs gfde gfdff fGgd gdsU hgfgh Sgdf fgdD dfgG [root@linuxprobe test]# cat b.txt RDFS GFSDG DFDS fsFS dfGGF rewr FGDF GFDHG FSD [root@linuxprobe test]# cat a.txt | python3 -c "import sys; print(sys.stdin.read().title())" Asfe Geri Fsddj Dsfs Gfde Gfdff Fggd Gdsu Hgfgh Sgdf Fgdd Dfgg [root@linuxprobe test]# cat b.txt | python3 -c "import sys; print(sys.stdin.read().title())" Rdfs Gfsdg Dfds Fsfs Dfggf Rewr Fgdf Gfdhg Fsd [root@linuxprobe test]# python3 -c "import sys; print(sys.stdin.read().title())" < a.txt Asfe Geri Fsddj Dsfs Gfde Gfdff Fggd Gdsu Hgfgh Sgdf Fgdd Dfgg [root@linuxprobe test]# python3 -c "import sys; print(sys.stdin.read().title())" < b.txt Rdfs Gfsdg Dfds Fsfs Dfggf Rewr Fgdf Gfdhg Fsd