linux 中实现大小写转换

 

1、tr实现

a、

[root@PC1 test1]# ls
a.txt
[root@PC1 test1]# cat a.txt                          ## 测试文本
ab cd EF
Gh ij KL
[root@PC1 test1]# cat a.txt | tr "A-Z" "a-z"         ## 全部转换为小写
ab cd ef
gh ij kl
[root@PC1 test1]# cat a.txt | tr "a-z" "A-Z"         ## 全部转换为大写
AB CD EF
GH IJ KL

 

b、大小写相互转换

[root@PC1 test1]# ls
a.txt
[root@PC1 test1]# cat a.txt                           ## 测试文本
ab cd EF
Gh ij KL
[root@PC1 test1]# cat a.txt | tr "a-zA-Z" "A-Za-z"    ## 大小写相互转换
AB CD ef
gH IJ kl

 

 

2、awk实现

[root@PC1 test1]# ls
a.txt
[root@PC1 test1]# cat a.txt                             ## 测试文本
ab cd EF
Gh ij KL
[root@PC1 test1]# awk '{print toupper($0)}' a.txt       ## 全部转换为大写
AB CD EF
GH IJ KL
[root@PC1 test1]# awk '{print tolower($0)}' a.txt       ## 全部转换为小写
ab cd ef
gh ij kl

 

3、sed实现

[root@PC1 test1]# ls
a.txt
[root@PC1 test1]# cat a.txt                      ## 测试数据
ab cd EF
Gh ij KL
[root@PC1 test1]# sed 's/[a-z]/\U&/g' a.txt      ## 全部转换为大写
AB CD EF
GH IJ KL
[root@PC1 test1]# sed 's/[A-Z]/\L&/g' a.txt      ## 全部转换为小写
ab cd ef
gh ij kl

 。

 

posted @ 2024-02-06 17:09  小鲨鱼2018  阅读(122)  评论(0编辑  收藏  举报