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

 

  

        

posted on 2018-11-06 16:31  kingle-l  阅读(262)  评论(0编辑  收藏  举报

levels of contents