酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Shell脚本:提取poem.txt中5 - 10个字符单词并按长度分类保存

以下是一个解决此问题的 shell 脚本: ```bash #!/bin/bash for len in 5 6 7 8 9 10 do count=0 output_file="word_len-$len.txt" echo "This file contains the number of words with length $len" > "$output_file" while read line do IFS=' ' read -ra words <<< "$line" for word in "${words[@]}" do if [[ ${#word} -eq $len ]] then ((count++)) echo "Line: $(awk 'NR==FNR{if(NR==1) line=$0; next} NR==FNR{next} NR==2{print NR - 1; exit}' poem.txt poem.txt), Word: $word" >> "$output_file" fi done done < poem.txt echo "Total number of words with length $len: $count" >> "$output_file" done ``` 此脚本假设 `poem.txt` 文件存在并且具有可读的内容。在实际使用中,您需要根据文件的实际路径和内容进行调整。 ######[AI写代码神器 | 266点数解答 | 2024-10-27 18:24:44]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]