酷代码 AI
菜单
服务商

编写shell脚本,打印出poem.txt文件中所有长度为5, 6, 7, 8, 9和10的单词,并分别将对应长度的单词保存到word_len-x.txt的文件中。同时,每个输出文件的内容有如下要求: 1. 第一行需要指出文件包含多少个长度为x的单词。 2. 每一个单词都需要打印其所在的行号

以下是一个解决此问题的 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` 文件存在并且具有可读的内容。在实际使用中,您需要根据文件的实际路径和内容进行调整。 [2024-10-27 18:24:44 | AI写代码神器 | 266点数解答]

相关提问
实用工具查看更多
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]
数独游戏 [娱乐类]
经典推箱子 [娱乐类]
AI摸鱼五子棋 [娱乐类]