zoukankan      html  css  js  c++  java
  • 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
    

      

            

  • 相关阅读:
    《R语言入门与实践》第七章:程序
    《R语言入门与实践》第六章:R 的环境系统
    《R语言入门与实践》第五章:对象改值
    《R语言入门与实践》第四章:R 的记号体系
    pandas包的应用
    numpy包的应用
    redis
    面试题
    qqqqqqq
    qqqqqqqqqqqqq
  • 原文地址:https://www.cnblogs.com/kingle-study/p/9916099.html
Copyright © 2011-2022 走看看