修复前面提到的问题(重新递增i
和误用cat
)会导致如下情况。请注意,该行date > file_$i
用于调试,以确保每个输出文件在测试开始时都是新的。:
操作员是无操作的。该表格<<<
引入了“here-doc”。如果 的内容$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.