
私はbook_1、book_2、... book_10という連続した名前のフォルダを10個持っていて、各フォルダにはフォルダと同じ名前のtxtファイルがあります。例えばbook_1にはのみbook_1.txt には歴史資料(テキストのみ)が含まれています。
出力を順番に出力ファイルに追加する AWK スクリプトを実行する必要があります。フォルダー全体を実行し、各フォルダーから必要なファイルを抽出するループを生成するにはどうすればよいでしょうか。
awk '
{
script//
}
END { print "The output of book num $i is: " m }' book*/book*.txt >> output.txt // m is a variable which extracts max occurences of certain words which are set in the script
出力は次のようになります。
The output of book num 1 is : 123
The output of book num 2 is : 2223
等々
助けてくれてありがとう!
答え1
{1..10}
数値の範囲を生成するための構造をサポートするシェルで任意の awk を使用する簡単な方法$(seq 10)
は次のとおりです (そうでない場合は のみを使用します)。
for i in {1..10}; do
awk -v i="$i" '
{
script//
}
END { printf "The output of book num %d is : %d\n", i, m }
' "book_${i}/book_${i}.txt"
done > output.txt
しかし、すべてを awk で実行したい場合は、次のようになります (ARGIND と ENDFILE に GNU awk を使用)。
awk '
BEGIN {
for (i=1; i<=10; i++) {
ARGV[ARGC] = "book_" i "/book_" i ".txt"
ARGC++
}
}
{
script//
}
ENDFILE { printf "The output of book num %d is : %d\n", ARGIND, m; m=0 }
' > output.txt
「book」ファイルのいずれかが存在できない場合は、それに対する何らかの保護を追加する必要があります。