打印字母数小于8的单词

打印字母数小于8的单词

写一个bash脚本以统计一个文本文件nowcoder.txt中字母数小于8的单词。
示例:
假设 nowcoder.txt 内容如下:

how they are implemented and applied in computer

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

and

applied

in

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

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 方法 1
awk -F " " '{for(i=1;i<=NF;i++) { if(length($i) < 8) { print($i) }}}' nowcoder.txt

# 方法 2
words=$(cat nowcoder.txt)
for i in ${words[*]}; do
if [ ${#i} -lt 8 ]; then
echo ${i}
fi
done

# 方法 3
for i in $(cat nowcoder.txt); do
if [ ${#i} -lt 8 ]; then
echo ${i}
fi
done

(本文完)

在线题目

https://www.nowcoder.com/practice/bd5b5d4b93a04226a81afbabf0be797d


打印字母数小于8的单词
https://maojun.xyz/blog/2023/11/打印字母数小于8的单词.html
作者
毛 俊
发布于
2023年11月8日
许可协议