Chinaunix

标题: 求大神帮忙 [打印本页]

作者: 凉风真    时间: 2016-05-04 23:00
标题: 求大神帮忙
2.        使用for循环将下面这段话中所有字母长度超过5的单词打印出来。
“When you're attracted to someone it just means that your subconscious is attracted to their subconscious, subconsciously.
So what we think of as fate, is just two neuroses knowing they're a perfect match.”
作者: ken.wlh    时间: 2016-05-23 09:19
#!/bin/sh
FILE="When you're attracted to someone it just means that your subconscious is attracted to their subconscious, subconsciously.So what we think of as fate, is just two neuroses knowing they're a perfect match"
NUM=`echo $FILE | wc -w`
for ((i=1;i<=$NUM;i++))
do
WORD=`echo $FILE | awk '{print $'$i'}'`
CNUM=`echo $WORD | wc -c`
if [ $CNUM -gt 6 ];
then
echo $WORD
fi
done

作者: 杰瑞26    时间: 2016-08-11 09:06
本帖最后由 杰瑞26 于 2016-08-11 09:10 编辑

  1. #!/bin/bash
  2. # filename: test.sh

  3. sentence="When you're attracted to someone it just means that your subconscious is attracted to their subconscious, subconsciously.
  4. So what we think of as fate, is just two neuroses knowing they're a perfect match."

  5. for word in ${sentence}
  6. do
  7.     new=`echo $word | tr -cd '[a-zA-Z]'`  # 去除句子中的 ,或者'
  8.     len=${#new}                           # 求长度
  9.     if [ "$len" -ge 5 ]                   # 再判断
  10.     then
  11.         echo $new
  12.     fi
  13. done
复制代码
结果输出:
  1. root@localhost:~/training# ./test.sh
  2. youre
  3. attracted
  4. someone
  5. means
  6. subconscious
  7. attracted
  8. their
  9. subconscious
  10. subconsciously
  11. think
  12. neuroses
  13. knowing
  14. theyre
  15. perfect
  16. match
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2