zoukankan      html  css  js  c++  java
  • 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确保只有首字母是大写  (参考:https://mp.weixin.qq.com/s?__biz=MzU3NTgyODQ1Nw==&mid=2247496957&idx=2&sn=ef565a4b741fca870e87f3ec1ac3ae50&chksm=fd1f8c7bca68056d810b480ed216c82d6ca5d69443dedd1fae9c76994fcb5f08f1ca91420590&mpshare=1&scene=23&srcid=10142uwg8zhqVivMSC0vjpp9&sharer_sharetime=1602637881416&sharer_shareid=4ed060cc4cd1efce40e3ab6dd8d8c7d4#rd)

    [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
  • 相关阅读:
    腾讯云 ubuntu 上tomcat加载项目很慢
    ubuntu 中iptables
    linux ssh修改 默认22端口
    jetty 客服端 与服务端
    spring mvc 拦截器
    linux配置iptables(3)
    el取bean 对象属性规则
    mybatis 反射bean规则
    fastjson tojson部分规则
    正则获取参数 分组
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/13800147.html
Copyright © 2011-2022 走看看