首页 > 试题广场 >

打印字母数小于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
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)
#!/bin/bash
awk '{
    for(i=0; i<=NF; i++) {
        if (length($i) < 8) print $i
    }
}'

发表于 2024-07-13 22:58:33 回复(0)
# 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 i in $(cat nowcoder.txt)
do
    [ ${#i} -lt 8 ] && echo $i
    continue
done
有没有大佬解释一下,不加continue 说我越界或者语法错误,这是为啥
发表于 2024-01-31 19:25:08 回复(0)
#!/bin/bash
count=1
while read words
do
    if [ $count -ne 1 ];then
    echo ""
    fi

    echo "行${count}字母数小于8的单词有:"
    ((count++))

    for i in $words
    do
        if [ ${#i} -lt 8 ];then
           echo -e "$i \c"
        fi
    done
done < $1
echo -e "\nEND"
命令:. ./shell.sh +filepath
输出:指定文档的所有字母数小于8的单词
发表于 2022-11-09 15:36:41 回复(0)
cat nowcoder.txt | awk '{if(length($i)<8){print $i}}'
发表于 2022-10-11 20:28:45 回复(0)
awk '{for(i=1;i<=NF;++i) { if(length($i) < 8) {print $i} }}' nowcoder.txt
发表于 2022-08-27 17:29:53 回复(0)
for i in $(cat nowcoder.txt)
do
 if [[ ${#i} -lt 8 ]]
 then
    echo $i
 fi
done
发表于 2022-07-22 10:06:51 回复(0)
sed -i 's/ /\n/g' nowcoder.txt && awk 'length ($0) < 8' nowcoder.txt

大佬们,为啥我这样写系统不通过呢? 我在自己的设备上测出来输出结果明明是一样的,这是为什么呢

发表于 2022-07-17 06:47:43 回复(0)
grep -Eo '[a-z]+' nowcoder.txt | awk '{ if (length($0)<8){print $0}}'

发表于 2022-07-13 10:38:50 回复(0)
awk -F ' ' '{for(i=1;i<=NF;i++){if(length($i)<8)print $i}}' nowcoder.txt
发表于 2022-07-11 19:29:08 回复(0)
#!/bin/bash
for i in $(cat nowcoder.txt)
do
a=${i:7:1}
if [ -z $a ];then
    echo $i
fi
done
发表于 2022-07-11 11:03:39 回复(0)
#!/bin/bash

for word in `cat nowcoder.txt`
do
    cnt=$[`echo $word | wc -c`-1]
    [ $cnt -lt 8 ] && echo $word
done
    
在我自己系统上没问题,在牛客上提交老是抱错,什么鬼

发表于 2022-06-18 17:44:55 回复(0)
awk -v RS=" " '{if(length($0)<8) print $0}' nowcoder.txt
发表于 2022-06-08 00:50:29 回复(0)
awk 'BEGIN{RS=" "} length($0) < 8 ' nowcoder.txt
发表于 2022-05-28 17:07:11 回复(0)
awk '
{
    s = $0
    n = length(s)
    l = 1;
    for (i = 1; i <= n; i++) {
        c = substr(s, i, 1);
        if (c !~ /[a-z]/) {
           len = i - l
           t = substr(s, l, len)
           if (len < 8) print(t)
           l = i + 1
        }
    }
    last = substr(s, l)
    if (length(last) < 8) print(last)
}
'

发表于 2022-05-13 19:44:55 回复(0)
awk '{for(i=1;i<=NF;i++)if(length($i)<8) print $i}'

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

发表于 2022-05-09 12:00:15 回复(0)
awk  '{for(i=0;i<=NF;i++) if(length($i)<8) print $i}'  nowcoder.txt
发表于 2022-05-07 15:07:14 回复(0)
方法一:使用正则表达式:[a-zA-Z]{8,}作为if判断条件
awk 'BEGIN{FS="";RS=" ";ORS="\n"}{if(NF<8)print$0}' nowcoder.txt
方法二:使用awk预定义函数length($i)得到每个字段的长度
awk '{for(i=1;i<=NF;i++){if(length($i)<8)print $i}}' nowcoder.txt
方法三:指定RS按段落读取文件并指定FS空格为字段分隔符
awk 'BEGIN{FS="";RS=" ";ORS="\n"}{if(NF<8)print$0}' nowcoder.txt
发表于 2022-05-02 23:27:42 回复(0)