分割字符串为单词,遍历每个单词并且获得每个单词的长度,也就是shell获取字符串长度
### 获取字符串长度的方式 ele-字符串变量 # awk 的length()函数 # awk的NF变量 echo "${ele}" | awk -F"" '{print NF}' # wc -c 字节个数,一个英文一个字节,因此可用,但是换行符也会被算上, 中文编码方式不同结果也不同 # wc -m 字符个数 # wc -L line的长度 # ${#ele} [ele是字符串变量名] # expr length ${ele} awk '{for(i=1;i<=NF;i++) if(length($i)<8) print $i}' nowcoder.txt cat nowcoder.txt | awk '{for(i=1;i<=NF;i++) if(length($i)<8) print $i}' ### 已下都用方法/函数定义 function test0() { for ele in `cat nowcoder.txt`; do if [ ${#ele} -lt 8 ]; then echo ${ele} fi done } # tem=`cat now/coder.txt` # arr=(${tem}) function test1() { for ele in ${arr[@]}; do if [ ${#ele} -lt 8 ]; then echo ${ele} fi done } function test11() { for ele in ${arr[@]}; do #temp=$(echo -n "${ele}" | wc -c) # -n参数:去除"\n"换行符,不去除的话,默认带换行符,字符个数就肉眼看到的多一个 temp=`echo -n "${ele}" | wc -c` if [ ${temp} -lt 8 ]; then echo ${ele} fi done } function test111() { for ele in ${arr[@]}; do temp=`expr length ${ele}` if [ ${temp} -lt 8 ]; then echo ${ele} fi done } function test1111() { for ele in ${arr[@]}; do temp=`echo "${ele}" | wc -L` if [ ${temp} -lt 8 ]; then echo ${ele} fi done } function test2() { local ele="" for (( i=0; i<${#arr[@]}; i++ )); do ele=${arr[${i}]} if [ ${#ele} -lt 8 ]; then echo ${ele} fi done } test0