首页 > 试题广场 >

打印字母数小于8的单词

[编程题]打印字母数小于8的单词
  • 热度指数:56981 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
写一个bash脚本以统计一个文本文件nowcoder.txt中字母数小于8的单词。
示例:
假设 nowcoder.txt 内容如下:
how they are implemented and applied in computer

你的脚本应当输出:
how
they
are
and
applied
in

说明:
不用担心你输出的空格以及换行的问题
示例1

输入

how they are implemented and applied in computer

输出

how
they
are
and
applied
in
cat nowcoder.txt | awk '{
for (i=1;i<=NF;i++){
        if (length($i) < 8)
                print $i
}
}'

发表于 2020-11-16 17:57:48 回复(0)

for i in `awk '{print}' nowcoder.txt`
do
    if [ ${#i} -lt 8 ]
    then
        echo $i
    fi
done

发表于 2021-02-13 12:06:55 回复(0)
awk 'BEGIN{FS="";RS=" ";ORS="\n"}{if(NF<8)print$0}' nowcoder.txt
发表于 2021-01-26 17:27:01 回复(3)
tr " " "\n" < nowcoder.txt | awk '/^.{0,7}$/'
发表于 2021-01-27 02:22:17 回复(0)
awk '{for(i=1;i<=NF;i++)if(length($i)<8)print $i;}' nowcoder.txt
发表于 2021-04-12 13:30:13 回复(0)
for i in `cat nowcoder.txt`
do
    if [ $(echo $i |wc -L ) -lt 8 ];then
        echo $i
    fi
done
发表于 2020-12-27 22:06:09 回复(0)
grep -Eo '\b\w{,7}\b' nowcoder.txt
发表于 2021-11-19 23:08:41 回复(0)
cat nowcoder.txt | xargs -n 1 | awk 'length($1)<8 {print $1}'

发表于 2021-05-08 20:55:32 回复(1)
cat nowcoder.txt | tr ' ' '\n' | awk 'length($0) < 8 {print $0}'

发表于 2024-10-30 10:18:42 回复(0)

awk 'BEGIN{RS="[[:space:]]+"}length($0)<8{print $0}' nowcoder.txt


发表于 2021-05-19 13:05:04 回复(1)
for i in `cat nowcoder.txt`
do
if [ `echo $i | wc -m` -lt 9 ]
then
echo $i
fi
done
换行符也是个字符

发表于 2021-01-22 17:36:23 回复(1)
# echo "how they are implemented and applied in computer systems systemsa" | sed 's/ /\n/g' | awk '/^.{1,7}$/'
cat nowcoder.txt | sed 's/ /\n/g' | awk '/^.{1,7}$/'

编辑于 2024-03-01 12:19:32 回复(0)
#!/bin/bash

for word in `cat $nowcoder`
do
#echo ${#word}
if [ ${#word} -lt 8 ]
then
echo $word
fi
done
发表于 2023-08-14 17:09:59 回复(0)
xargs -n1 | awk 'length($1) < 8 {print $1}'

发表于 2023-08-04 22:13:38 回复(0)
awk  '{for(i=0;i<=NF;i++) if(length($i)<8) print $i}'  nowcoder.txt
发表于 2022-05-07 15:07:14 回复(0)
为什么  [[ 12 < 9 ]];echo $?  返回结果是0 true

for i in `cat nowcoder.txt`;do if [ $(echo $i | wc -m) -lt 9 ] ;then echo $i;fi;done  #换行符占一个
for i in `cat nowcoder.txt`;do if (($(echo $i | wc -L)<8)) ;then echo $i;fi;done
for i in `cat nowcoder.txt`;do if (($(expr length $i)<8));then echo $i;fi;done
for i in `cat nowcoder.txt`;do if((${#i}<8));then echo $i;fi;done
awk '{for(i=1;i<=NF;i++) if(length($i)<8) print $i }' nowcoder.txt
xargs -n1 -a nowcoder.txt | while read p; do if (($(echo $p | wc -L)<8)); then echo $p; fi; done


编辑于 2021-06-03 20:20:41 回复(1)
awk '{for(i=1;i<=NF;i++)if(length($i)<8)print $i}'

发表于 2025-11-07 15:58:10 回复(0)
grep -Eow '\w{,7}' nowcoder.txt
发表于 2025-10-28 16:58:14 回复(0)
for i in $(cat nowcoder.txt); do
    if [ $(echo -n $i | wc -m) -lt 8 ]; then
        echo "$i"
    fi
done
echo -n 不计入后面/n  
发表于 2025-06-25 10:57:34 回复(0)
#!/bin/bash

cat nowcoder.txt | awk '{ for(i=1;i<=NF;i++) if(length($i)<8) print $i }'

发表于 2025-06-09 08:48:55 回复(0)