linux系統基礎

大不美列坚發表於2024-09-07

image
image
image
image

#!/bin/bash

# 確保提供了檔名
if [ "$#" -ne 1 ]; then
  echo "Usage: $0 filename"
  exit 1
fi

filename=$1

# 篩選出包含至少四個'a'且不以's'結尾的單詞
grep -E 'a.*a.*a.*a' "$filename" | grep -v 's$' > filtered_words.txt

# 統計每個單詞的出現頻率並排除檔案中已經出現的單詞
sort filtered_words.txt | uniq -c | sort -nr > word_counts.txt

# 提取出現頻率前三的單詞
top_three_words=$(head -n 3 word_counts.txt | awk '{print $2}')

# 提取這些單詞的末尾三個字母
for word in $top_three_words; do
  echo "${word: -3}"
done | sort | uniq -c | sort -nr

image

相關文章