对于句子I am looking for you,通过shell脚本实现查找出单词长度不大于3的单词,请尽量使用较多的方法实现。
一、数组实现
1、法一
#!/bin/sh arr=(I am looking for you) for word in ${arr[*]} do if [ ${#word} -le 3 ] then echo $word fi done
2、法二
#!/bin/sh arr=(I am looking for you) for ((i=0;i<${#arr[*]};i++)) do if [ ${#arr[$i]} -le 3 ] #子串长度计算 then echo ${arr[$i]} fi done
二、字符串实现
#!/bin/sh str_="I am looking for you" for word in $str_ do if [ ${#word} -le 3 ] then echo $word fi done
三、awk命令实现
[root@localhost ~]# echo "I am looking for you"|awk '{for(i=1;i<=NF;i++) if(length($i)<=3) print $i}' I am for you