我有几个看起来像这样的数据文件:
HR0
012312010
001230202
HR1
012031020
012320102
012323222
012321010
HR2
321020202
...
要说明:有一行定义了字段(HR“ n”),具有四进制数的可变数量的行(321020202),然后是两个字段之间的额外换行符。我想合并等效的HR字段。因此从某种意义上讲,我想将这些文件压缩为一个大文件。我认为使用sed是答案,但是我不知道从哪里开始。
我正在考虑在python或c ++程序上使用shell脚本,因为我认为它在编写和执行方面可能会更快。有什么想法吗?
这在C ++中很容易做到,如果您拥有C ++ 17,则可以做得更多。您可以编写一个用于读取multimap<int, int>
类似内容的函数:
multimap<int, int> read(istream& input) {
multimap<int, int> output;
string i;
while(input >> i) {
const auto key = std::atoi(data(i) + 2);
transform(istream_iterator<int>(input), istream_iterator<int>(), inserter(output, begin(output)), [key](const auto value){ return make_pair(key, value); });
input.clear();
}
return output;
}
因此,您将在每个文件的位置调用该函数,ifstream
并将其用于merge
将收益转储到要计算的multimap<int, int> output
。
然后,您将转储output
到您的输出文件,说它已经被打开了,ofstream filep
您可以像这样转储:
auto key = cbegin(output)->first;
filep << key << ":\n" << setfill('0');
for(const auto& it : output) {
if(it.first == key) {
filep << '\t' << setw(9) << it.second << endl;
} else {
key = it.first;
filep << key << ":\n\t" << setw(9) << it.second << endl;
}
}
我在这里写了一个仅涉及一个文件的实时示例:http : //ideone.com/n47MnS
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句