首页 > 试题广场 >

打印字母数小于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
头像 阿尔可
发表于 2021-08-26 22:33:25
用空格进行分割,NF是当前记录的字段数,也可以说是单词数;然后for循环嵌套if判断;当当前字段的长度小于8时,将其打印出来; #!/bin/bash awk -F" " '{for(i=1;i<=NF;i++){if(length($i) < 8){print $i 展开全文
头像 郭富成
发表于 2021-07-29 17:16:34
分割字符串为单词,遍历每个单词并且获得每个单词的长度,也就是shell获取字符串长度 ### 获取字符串长度的方式 ele-字符串变量 # awk 的length()函数 # awk的NF变量 echo "${ele}" | awk -F"" '{print 展开全文
头像 bug_making()
发表于 2022-04-27 11:32:06
先使用 xargs 命令将输出转换为单行,然后指定 awk 的分隔符为 "",设定每行的筛选条件为 NF<8 #!/bin/bash cat nowcoder.txt | xargs -n1 | awk -F "" 'NF<8'
头像 牛客8251995号
发表于 2021-08-02 11:53:59
#!/bin/bash words=$(cat nowcoder.txt) for word in ${words} do if [ ${#word} -lt 8 ];then echo ${word} fi done
头像 有个
发表于 2021-12-19 00:57:50
awk -F " " '{for(i=1;i<=NF;i++){if(length($i) < 8) print $i}}' nowcoder.txt for i in $(cat nowcoder.txt); do if [ ${#i} -lt 8 ]; then 展开全文
头像 AAA批发电锯
发表于 2023-06-06 13:09:33
#!/bin/bash #空格分割,数组存单词,统计单词长度判断后输出单词 IFS=" " words=($(cat nowcoder.txt)) for word in ${words[@]} do [ ${#word} -lt 8 ] && echo $word done < n 展开全文
头像 牛客612055456号
发表于 2022-05-16 14:41:50
#!/bin/bash for i in $(cat nowcoder.txt);do if [[ ${#i} -lt 8 ]] ; then echo $i fi done
头像 求球
发表于 2022-02-24 12:03:39
awk '{split($0,a," ");for(i in a){if(length(a[i])<8) print a[i]}}' nowcoder.txt
头像 wnlg辞
发表于 2022-01-05 17:23:25
tr ",." " " < nowcoder.txt | awk '{for(i=1;i<=NF;i++) {if(length($i) < 8) print&n 展开全文
头像 拔个牙咯
发表于 2022-07-19 17:48:11
三种方式可以实现 cat nowcoder.txt | sed 's/ /\n/g' | awk '{if(length($0)<8) print}' cat nowcoder.txt 展开全文