複数のファイルを必要とするbashスクリプトを書く方法は?

複数のファイルを必要とするbashスクリプトを書く方法は?

私は複数のファイルを入力として使用し、各ファイルに対して最も頻繁に発生する上位の「n」単語を降順に表示するbashスクリプトを作成しています。

1つのファイルの単語頻度を計算する方法を見つけましたが、複数のファイルがあるため、並列に処理するときにどのように処理するのかわかりません。

 sed -e 's/[^[:alpha:]]/ /g' testfile.txt | tr '\n' " " |  tr -s " " | tr " " '\n'| tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | nl 

これは1つのファイルに対してうまく機能しますが、次のように実行できるbashスクリプトを作成したいと思います。

$countWord test1.txt test2.txt test3.txt (countword here is my bash script that counts freq)

このファイルを入力として使用し、各ファイルについて次の内容を表示したいと思います。

   ===(1 51 33 test1.txt)====    # where 1: number of lines, 51: number of words, 33: number of characters
38 them
29 these
17 to
12 who

 

正しい方向で助けてくれてありがとう。 :)

ベストアンサー1

ファイルのループの作成

for F in "$@"
do echo "=== $F ==="
    sed -e 's/[^[:alpha:]]/ /g' "$F" | tr '\n' " " |  tr -s " " | tr " " '\n'| tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | nl
done

楽しくお過ごしください!

おすすめ記事