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]
- Python 代码尝试采集唐诗三百首内容时遇“服务器繁忙”困境(GPT | 15点数解答 | 2024-06-24 15:46:11)378
- 测3:以读、二进制方式打开C盘temp文件夹下abc.txt文件的代码选哪个?(GPT | 15点数解答 | 2024-06-06 13:53:37)416
- Linux 终端操作全攻略:用户创建、文件处理、权限设置与日志查找(字节豆包 | 592点数解答 | 2025-06-12 21:19:29)191
- Python实现:轻松爬取古诗网唐诗信息(GPT | 316点数解答 | 2024-06-24 00:59:22)321
- 网站密码有效性检查:代码实现与详细解读(字节豆包 | 567点数解答 | 2025-11-26 20:44:53)89
- Linux 知识问答:判断文件 test.txt 是否存在的正确命令揭晓!(字节豆包 | 62点数解答 | 2024-11-22 17:14:08)305
- 优化代码:安全高效解锁设备 Bootloader 与刷入 Recovery 镜像(DeepSeek | 345点数解答 | 2025-02-21 22:47:48)185
- Python实现多功能文字处理软件:含代码、解释与注意事项(DeepSeek | 449点数解答 | 2025-12-18 18:01:25)60
- 批处理文件:依据参数表.xlsx精准修改文件.txt中p、t、s值(GPT | 811点数解答 | 2024-12-05 12:58:37)415
- 用批处理和Python实现“参数表.xlsx”与“文件.txt”数据匹配及修改(讯飞星火 | 531点数解答 | 2024-12-05 13:20:42)389
- 批处理文件:依据参数表.xlsx精准修改文件.txt特定参数值(阿里通义 | 495点数解答 | 2024-12-05 13:22:26)367
- C语言实现:计算n个元素集合的非空子集划分数量(字节豆包 | 223点数解答 | 2025-04-02 08:32:25)244