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

linux 中大小写转换-ag真人游戏

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

 

网站地图