1

我有一个如下所示的文本文件:

valcred: requestValCred [up]
 certificate pki_001  [up]
 certificate pki_002  [up]
 certificate pki_003  [up]
 certificate pki_004  [up]
 certificate pki_005  [up]
valcred: internalValCred [up]
 certificate pki_021  [up]
 certificate pki_012  [up]
 certificate pki_103  [up]
 certificate pki_404  [up]
 certificate pki_555  [up]

每次出现“valcred”时,我都想将其拆分为一个新的文本文件。然后,我想将所有证书放入带有所述 valcred 的文件中。以下是一个文件的内容。这些文件中大约有 100 个,每个文件都有不同数量的条目。发生错误时,我得到大约 20 个文件。

valcred: internalValCred [up]
 certificate pki_021  [up]
 certificate pki_012  [up]
 certificate pki_103  [up]
 certificate pki_404  [up]
 certificate pki_555  [up]

我使用这个命令:

nawk '/valcred/{x="F"++i;}{print > x;}' input_file.txt  

但这就是问题所在。我遇到了这个错误:

bash-2.03$ nawk '/valcred/{x="F"++i;}{print > x;}' input_files.txt
nawk: F21 makes too many open files
 input record number 1743, file input_files.txt
 source line number 1

我以为我只需要关闭打印语句。没有什么对我有用。有人可以帮我添加一个密切的声明来使它起作用吗?还是替代解决方案?

4

2 回答 2

1

You need to close the output file before opening a new one. Also I expect you want to append >> to the file x rather then overwriting > it again and again:

nawk '/valcred/{close(x);x="F"++i;printf "" > x}{print >> x;}' input_file.txt

Also note, that I'm truncating the file on it's first usage using printf "" > x.

于 2014-06-12T19:12:24.997 回答
0

把它放在一个脚本文件中,然后在你想要分解的文本文件上运行它。

#!/usr/bin/awk -f

BEGIN { valnum=0 }

/^valcred/ {valnum++
            close(output)
            print $0 > valnum}
/^ certificate/ { print $0 > valnum }
于 2014-06-12T19:21:23.573 回答