$ cat ../tmp
1 t2
1 f3
1 end
2 t2
2 f2
2 end
3 t2
3 f3
4 t1
3 end
4 f2
4 end
$ awk '{if(c[$1] == 0)print >> $1} ($2~/t/ && $2!~/t2/) || ($2~/f/ && $2!~/f3/){if(c[$1] == 0)c[$1] = 1} $2~/end/{if(c[$1]==0) system("cat "$1" >> LOG")}' ../tmp
$ ls
1 2 3 4 LOG
$ cat 1
1 t2
1 f3
1 end
$ cat 2
2 t2
2 f2
$ cat 3
3 t2
3 f3
3 end
$ cat 4
4 t1
$ cat LOG
1 t2
1 f3
1 end
3 t2
3 f3
3 end
$ rm *
$ awk '{if(c[$1] == 0)print >> $1} ($2~/t/ && $2!~/t2/) || ($2~/f/ && $2!~/f3/){if(c[$1] == 0){system("rm "$1);c[$1] = 1}} $2~/end/{if(c[$1]==0) system("cat "$1" >> LOG")}' ../tmp
$ ls
1 3 LOG
$ cat 1
1 t2
1 f3
1 end
$ cat 3
3 t2
3 f3
3 end
$ cat LOG
1 t2
1 f3
1 end
3 t2
3 f3
3 end
ファイルディスクリプタ上限超過を避けるには、
awk '{if(c[$1] == 0)print >> $1} ($2~/t/ && $2!~/t2/) || ($2~/f/ && $2!~/f3/){if(c[$1] == 0){buf = system("rm "$1);c[$1] = 1;close(buf)}} $2~/end/{if(c[$1]==0){buf = system("cat "$1" >> LOG"); close(buf)}}' ../tmp