题目:找出字符串不大于6的单词"welcome to shell world"
方法一:用awk进行提取
$ echo "welcome to shell world"|awk -F" " '{for(i=1;i<=NF;i++)if(length($i)<7)print $i}'
方法二:用数组进行提取;直接从数组中提取
$ arr=(welcome to shell world)
$ for file in ${arr[@]};do if [ ${#file} -lt 7 ];then echo $file;fi;done
这里注意学习一下,[]、[[]]、()、(())、{}等的用法
方法三:用数组存放每个单词,每个单词判断一次长度是否小于6
$ for ((i=0;i<${#arr[*]};i++));do if [ ${#arr[$i]} -lt 7 ];then echo ${arr[$i]};fi;done
这篇博客介绍了如何在Shell中使用awk命令和数组来提取字符串中长度不超过6的单词。提供了三种不同的方法,包括awk的字段操作和数组遍历,强调了不同括号在Shell脚本中的用法。

4863

被折叠的 条评论
为什么被折叠?



