前述の問題 ( の再インクリメントi
と誤用cat
) を修正すると、次のような結果になります。date > file_$i
テストの開始時に各出力ファイルが新しいことを確認するために、この行はデバッグ用にあることに注意してください。:
オペレーターはノーオペレーションです。フォーム<<<
は「ヒアドキュメント」を導入します。の内容が$lines
ファイル名である場合、質問で指定されているドキュメントではなく、代わりに を使用<"$lines"
し<<<"$lines"
ます。
#!/bin/bash
i=1
while read line; do
date > file_$i
while read row; do
sed 's/\./.\n/g' <<< "$line" | grep -iB1 "$row" | tr -d '\n' | sed 's/--/\n/g' >> file_$i
done < $2
: $((i++))
done < $1
以下を含む splitdoc.data が与えられた場合:
This is doc 1. I am 1 fine. How are you, 1.? Ok. Hello 1.-- Go away now.
This is doc 2. I am 2 fine. How are you, 2.? Ok. Hello 2.-- Go away now.
This is doc 3. I am 3 fine. How are you, 3.? Ok. Hello 3.-- Go away now.
This is doc 4. I am 4 fine. How are you, 4.? Ok. Hello 4.-- Go away now.
および splitdoc.tags は次のとおりです。
How are you
Go away now
次に、コマンド
./splitdoc.sh splitdoc.data splitdoc.tags ; head file_*
生成:
==> file_1 <==
Fri Oct 26 19:42:00 MDT 2012
I am 1 fine. How are you, 1. Hello 1.
Go away now.
==> file_2 <==
Fri Oct 26 19:42:00 MDT 2012
I am 2 fine. How are you, 2. Hello 2.
Go away now.
==> file_3 <==
Fri Oct 26 19:42:00 MDT 2012
I am 3 fine. How are you, 3. Hello 3.
Go away now.