• shell 对字符的求长


    一,测试环境

    echo "To the world you may be one person but to one person you may be the world"
    
    
    对于世界而言,你是一个人;但是对于某个人,你是他的整个世界

    二 , 解

      01,数组

         判断一段里面小于4的字符串

    #!/bin/bash
    #################################################
    #    File Name: arrs01.sh
    #       Author: kingle
    #         Mail: kingle_work@163.com
    #     Function:
    # Created Time: 2018年11月06日 星期二 16时13分16秒
    #################################################
    array=(To the world you may be one person but to one person you may be the world)
    for word in ${array[@]}
    do
        if [ ${#word} -lt 4 ]
        then
            echo ${word}
        fi
    done

          操作结果  

    [root@kingle conpa]# sh arrs01.sh
    To
    the
    you
    may
    be
    one
    but
    to
    one
    you
    may
    be
    the
    

       02,数组

          实现代码

    #!/bin/bash
    #################################################
    #    File Name: arrs01.sh
    #       Author: kingle
    #         Mail: kingle_work@163.com
    #     Function:
    # Created Time: 2018年11月06日 星期二 16时13分16秒
    #################################################
    array=(To the world you may be one person but to one person you may be the world)
    for ((i=0;i<${#array[@]};i++))
    do
        if [ ${#array[i]} -lt 4 ]
        then
            echo ${array[i]}
        fi
    done

          操作结果     

    [root@kingle conpa]# sh arrs02.sh
    To
    the
    you
    may
    be
    one
    but
    to
    one
    you
    may
    be
    the
    

         

       03,awk  

    [root@kingle conpa]# echo "To the world you may be one person but to one person you may be the world"|awk '{for(i=1;i<=NF;i++)if(length($i)<4)print $i}'
    To
    the
    you
    may
    be
    one
    but
    to
    one
    you
    may
    be
    the

       04,awk

    [root@kingle conpa]# echo "To the world you may be one person but to one person you may be the world"|xargs -n1|awk '{if(length($1)<4)print $1}'
    To
    the
    you
    may
    be
    one
    but
    to
    one
    you
    may
    be
    the
    

        05.awk

          

    [root@kingle conpa]# echo "To the world you may be one person but to one person you may be the world"|awk '{for(i=1;i<=NF;i++)if(length($i)<4)print $i;else print "yes"}'
    To
    the
    yes
    you
    may
    be
    one
    yes
    but
    to
    one
    yes
    you
    may
    be
    the
    yes
    

      

            

  • 相关阅读:
    centos中文乱码修改字符编码使用centos支持中文
    java知识总结-26
    java知识总结-25
    java知识总结-24
    java知识总结-23
    java知识总结-22
    java知识总结-21
    java知识总结-20
    java知识总结-19
    java知识总结-18
  • 原文地址:https://www.cnblogs.com/kingle-study/p/9916099.html
Copyright © 2020-2023  润新知